Protocol Buffers with Objective-C

What are Protocol Buffers? Protocol Buffers are a way of encoding structured data. Protocol buffers are a flexible, efficient, automated mechanism for serializing structured data like XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages. You can even update your data structure without breaking deployed programs that are compiled against the “old” format.

How to use Protocol Buffers with Objective C?

To use protocol buffers with Objective C, follow the steps below:

1. First, we need to create a .proto file with a message in it. You can write a .proto file as :

message Person

{ required string name = 1;

required int32 id = 2;

optional string email = 3;

}

Xcode can be used to create a .proto file as follows:

1) Open Xcode

2) Create a new project.

3) Add files to this project ( Click File -> New -> New File). Go to Resources and click on Strings file.

4) Save the file with name Person.proto . Click “use .proto” in the alert pane that appear.

5) Now Person.proto must be added to your project. Write the above message in this file.

2. Now our next step is to compile this .proto file to produce objective C code.

For this we need the protocol buffer compiler, protoc To use protoc follow the steps:

1) Download and unzip the latest ProtocolBuffers-*.*-Source.tar.gz file.

2) Open Terminal and navigate to the folder where you have installed the files unzipped in previous step. (Navigate in terminal using command “cd” to go a particular path)

3) Enter following commands one by one: ./autogen.sh ./configure make

4) make test , to check if make is successful. If make test is unsuccessful try

“./configure –prefix=/usr ” in place of “./configure”.

By default prefix is /usr/local.

Then check if you see a new folder “src” in your install directory. Open it and you will find protoc there.

Now to compile the .proto file using protoc …… Invoke the compiler as follows :

Open terminal and type… protoc  –proto_path=src  –objc_out=build/gen  src/Person.proto

The parameter to –objc_out is the path of the directory where you want the output files and the parameter to –proto_path is the path of directory where your .proto file is present.

The last parameter is the complete path of the .proto file.

However, in case if you get the error that protoc is not a command, then try :

navigate to the folder /usr/bin in terminal and then type the above command

OR give the “complete path of protoc” in place of protoc in the above command like: /../../../src/protoc  –proto_path=/../../..  –objc_out=/../../.   /../Person.proto

The compiler will read the file Person.proto and generate two output files Person.pb.h and Person.pb.m that is a header and an implementation file in your output folder.

Then to use these files in your project,drag and add these two files to your Xcode project

OR If you want to compile the .proto file within your project just before it builds, you have to declare a custom script in XCode. And a custom script can be added as:

1)The Person.proto file must be present in your project directory.

2) In Xcode go to your project Target and click on Build Rules.

3) Click on the “+” sign and add new build rule.

4) Type *.proto in the text field next to “Source files with names matching:”

5) Choose “Custom script” from pop up button menu next to ‘Using’

6) Give your custom script in the text field below as: protoc  –proto_path=${PROJECT_DIR}   –objc_out=${DERIVED_FILES_DIR}   ${INPUT_FILE_PATH} OR the ‘complete path of protoc’ in place of ‘protoc’ whatever is working for you.

7) Now in the output files click on + sign and add two files and name them ${INPUT_FILE_BASE}.pb.h ${INPUT_FILE_BASE}.pb.m

8) Now go to Build Phases. Click on “compile sources” and add your Person.proto file to compile sources.

9) Compile the project.

3. Now, next step is to use these .pb.h and .pb.m files in our project.

Once you have added these files to your project you have to make several changes to build them.

1) Go to the directory where you have installed/unzipped the downloaded Protocol Buffer Source.

2) There you will find a folder “Objective C”. open it and you will find ProtocolBuffers Xcode project.

3) Add a reference to this project in your project. (You can do this by dragging ProtocolBuffers.xcodeproj to your project). I suggest adding it under frameworks.

4) Navigate to Build Phases of your target. There Open “Link Binary with libraries”. Now drag the libProtocolBuffers.a (found under the products of ProtocolBuffers.xcodeproj) to “Link binary with libraries”. It will then be added to your frameworks.

5) Now click on “Target dependencies” under Build Phases of your target. Click on the + sign and add ProtocolBuffers as your target dependency.

6) Now goto Build Setting of your target. In the search field type Header search paths. Then in Header search paths add a path to your “//objective C/Classes”. The classes folder is present in your downloaded protocol buffer source directory -> objective C folder -> classes. Add a reference to this folder in header search paths.

7) Compile the project. It must run smoothly without any errors.

4. The final step is To Create the Objective-C data from this protocol buffer message.

In your project’s AppDelegate.m file you may create the object of person as…

use Person.pb.h and Person.pb.m like this to produce raw protobuf data from a message

Person* person = [[[[[Person builder] setId:123] setName:@”abc”] setEmail:@” abc@example.com”] build];

NSData* data = [person data];

And to read the message from this raw protobuf data..

NSData* raw_data = data;

Person* person = [Person parseFromData:raw_data];

This way you can use protocol buffers with objective C.

Written By: Neha Gupta, Software Engineer, Mindfire Solutions

 

Advertisements

About Neha Gupta

Software Engineer at Noida, India, Work on MAC OSX Application development with Cocoa, iOS development and Qt (Cross Platform Application Framework)

Posted on January 28, 2014, in Cocoa Application, Objective-C, Protocol Buffers and tagged , , , , , , . Bookmark the permalink. 4 Comments.

  1. Hi,

    I tried the steps you have mentioned but am unable to proceed. When I run ./autogen.sh on the downloaded code it generates the following error after some execution

    ————————————————————
    Can’t exec “libtoolize”: No such file or directory at /usr/local/share/autoconf/Autom4te/FileUtils.pm line 345, line 6.
    autoreconf: failed to run libtoolize: No such file or directory
    autoreconf: libtoolize is needed because this package uses Libtool
    ————————————————————

    I’m using Brew package manager and have updated Libtool

    I’m using Mac OS 10.8.5 with Xcode 5.1

  2. Reblogged this on Developers Area.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: