How to Integrate Google Protocol Buffers into iOS Apps with Objective‑C

This guide explains why and how to use Google Protocol Buffers for efficient data exchange in iOS, covering environment setup, installation, .proto definition, compilation to Objective‑C files, project integration, and a complete encode/decode test with sample code.

Suishouji Tech Team
Suishouji Tech Team
Suishouji Tech Team
How to Integrate Google Protocol Buffers into iOS Apps with Objective‑C

Background

Because the Suishouji client requires high‑performance data transfer, traditional formats like JSON or XML are insufficient, so Google Protocol Buffers are adopted for efficient serialization.

Introduction

Protocol Buffers (https://developers.google.com/protocol-buffers/) is an open‑source, cross‑platform serialization format supported by many languages. It is smaller, faster, and simpler than XML/JSON. The syntax currently has two versions: proto2 and proto3. The latest official release is 3.5.1, and the tutorial is based on this version. Official documentation is at https://developers.google.com/protocol-buffers/docs/overview.

Prerequisites

Environment Requirements

Objective‑C 2.0 Runtime (32‑bit & 64‑bit iOS, 64‑bit macOS)

Xcode 7.0+

Note: Protobuf does not use ARC for performance, but it can be used under ARC.

Installation

Download the Protobuf source package (e.g., protobuf-objectivec-3.5.1.tar.gz).

Extract the archive.

Install required tools:

$ brew install autoconf
$ brew install automake
$ brew install libtool

Compile Protobuf:

$ ./autogen.sh
$ ./configure
$ make
$ make install

Verify installation: $ protoc --version Expected output:

libprotoc 3.5.1

Using Protobuf in iOS

Create a .proto File

Example Person.proto:

syntax = "proto3";

message Person {
  string name = 1;
  int32 id = 2;
  string email = 3;

  enum PhoneType {
    MOBILE = 0;
    HOME = 1;
    WORK = 2;
  }

  message PhoneNumber {
    string number = 1;
    PhoneType type = 2;
  }

  repeated PhoneNumber phone = 4;
}

Compile the file to Objective‑C sources: protoc Person.proto --objc_out=./ This generates Person.pbobjc.h and Person.pbobjc.m.

Import Protobuf Runtime Resources

Copy the following files from the extracted protobuf/objectivec directory into your project, excluding GPBProtocolBuffers.m:

All .h files under

objectivec/
objectivec/google/protobuf/*.pbobjc.h
objectivec/google/protobuf/*.pbobjc.m

All .m files under objectivec/ except the excluded one.

Because Protobuf does not use ARC, add the compiler flag -fno-objc-arc to every imported .m file.

Project structure (illustrative):

ProtobufDemo project structure
ProtobufDemo project structure
Note: Add $(PROJECT_DIR)/ProtocolBuffers to Header Search Paths.

Directly Add ProtocolBuffers Project

If manual file copying is cumbersome, you can add the ProtocolBuffers Xcode project as a dependency:

Copy all files from protobuf/objectivec into ProtobufDemo/ProtocolBuffers.

In the ProtobufDemo project, add the ProtocolBuffers_iOS sub‑project.

Configure Build Phases to link required libraries.

Import Person.pbobjc.h and Person.pbobjc.m and apply -fno-objc-arc to the .m files.

Update Header Search Paths to $(PROJECT_DIR)/ProtocolBuffers.

Build Phases configuration
Build Phases configuration

Testing

Import the generated header: #import "Person.pbobjc.h" Create and encode a Person object, then decode it:

Person *p = [[Person alloc] init];
 p.id_p = 1;
 p.name = @"person1";
 p.email = @"[email protected]";
 // encode
 NSData *data = [p data];
 NSLog(@"Protocol Buffers:
%@
Data: %@
Data Length: %lu", p, data, data.length);
 // decode
 Person *newP = [[Person alloc] initWithData:data error:nil];
 NSLog(@"Decoded: %@", newP);

Running the app prints:

Protocol Buffers:
<Person 0x60c0000da2b0>: {
    name: "person1"
    id: 1
    email: "[email protected]"
}
Data: <0a07706572736f6e3110011a0a3132334071712e636f6d>
Data Length: 23
Decoded: <Person 0x6040000d9c90>: {
    name: "person1"
    id: 1
    email: "[email protected]"
}

Enjoy the compact and fast serialization provided by Protocol Buffers!

mobile developmentiOSserializationProtocol BuffersObjective‑C
Suishouji Tech Team
Written by

Suishouji Tech Team

Suishouji's official tech channel, sharing original technical articles, posting recruitment opportunities, and hosting events. Follow us.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.