Google Protocol Buffers on iOS

Google protocol buffers on iOS

Ok. It appears that the metasyntactic library (or any other 3rd party library) is unnecessary in this case. You can just add the Google source directly to your project. I found the following answer from Nicola Ferruzzi in a google discussion group . . .

The original answer is here . . .

http://groups.google.com/group/protobuf/browse_thread/thread/ca4218d7db144252

The content of this answer is included below with images to make a permanent record ...


EDIT

Since trying this again tonight for the first time in a while, I needed a couple more steps in addition to those outlined below (this works with protobuf 2.5.0).

  • You'll need to link against libz.dylib. You set this in Build Phases > Link Binary With Libraries.
  • To easily remove all of the unit test related stuff, use the following command from the shell in the google directory find . -name "*unittest*" -exec rm -rf {} \;
  • Also delete the folder called testing
  • comment out #include <google/protobuf/testing/googletest.h> in stringprintf.cc
  • Now carefully follow the instructions below and all should work fine.

I use latest version in my app .. you don't really need objc direct
support if you are familiar with C++, there is only one point where
you have to pass from std::string to NSData and viceversa. And its
pretty simple.

To compile and test the easiest way Ive found is to just import the
whole google directory in my own project :) (the second time you can
make your own framework but for testing this procedure just works)

  • download latest version
  • autogen configure and make like you were just building for macosx (you need command line tools) . This way you end up with protoc

    binary and the library for macosx (which you don't need)
  • open your Xcode iOS project
  • add "new file" to your project and select google directory
  • add the directory of google headers to your additional include directories
  • add config.h from the protobuffer src directory to your app
  • from the google group remove everything that contains unitest :)
  • from the google group remove compiler and java stuff;

You should be able to compile without any linking error. To give you
an idea this is what I directly compile

Sample Image

Then you can use protoc to generate c++ source files for your
protocol. To use them with objc you have to rename your source to
file "mm" then you can do something like

TO SERIALIZE TO NSDATA


let's say your message is called Packet

- (NSData *)getDataForPacket:(Packet *)packet { 
std::string ps = packet->SerializeAsString();
return [NSData dataWithBytes:ps.c_str() length:ps.size()];

TO READ FROM NSDATA

- (Packet *)getPacketFromNSData:(NSData *)data { 
char raw[[data length]];
Packet *p = new Packet;
[data getBytes:raw length:[data length]];
p->ParseFromArray(raw, [data length]);
return p;

}

Integrate Google Protocol Buffers with Xcode project

You may add the include path of Google Protocol Buffer in Xcode's User Header Search Path.

Sample Image

Google Protobufs in iOS app crashing at GPBDescriptor

of course immediately after I post the question I find the answer...
Google protocol buffers on iOS

Needed to add -fno-objc-arc to every ...pbobjc.m file in the Compile Sources of Build Phases.
I had already done this for a few files in Dev, hence the (very subtle!) difference.

Google Protocol Buffers on iOS/Objective-C

I have used it to some extent on iOS, and with metasyntactic's extension, it works really well. I even managed to get the code generation as a custom build step in Xcode. We switched to Thrift for our project (for other reasons), so my apologies if some details below are wrong, but in general this is how to do it.

  1. In Xcode 4.2, open the target properties, go to the "Build Rules" tab and create a new build rule for "*.proto" files
  2. In "Using:", choose "custom script" and use a script like this:

    protoc --plugin=/usr/local/bin/protoc-gen-objc $INPUT_FILE_PATH --objc_out=$DERIVED_FILES_DIR

  3. In Output files, add the generated files (there should be two files, $(INPUT_FILE_BASE).pb.m and $(INPUT_FILE_BASE).pb.h.

  4. Add the .proto files to your project.

Adding C++ Google Protocol Buffers to iOS app is failing to load headers from Header Search Path

dont give the path till google/ ... give the path till src/

Navigate to your protocol buffer's src folder and just drag and drop the src folder to "Header Search Paths" in XCode buld settings. That should do it.



Related Topics



Leave a reply



Submit