Marc Wandschneider
f03d2668bc
|
10 years ago | |
---|---|---|
.. | ||
GRPCClient | 10 years ago | |
ProtoRPC | 10 years ago | |
RxLibrary | 10 years ago | |
examples/Sample | 10 years ago | |
.gitignore | 10 years ago | |
README.md | 10 years ago |
README.md
gRPC for Objective-C
- Install protoc with the gRPC plugin
- Use protoc to generate a gRPC library
- Integrate the generated gRPC library in your project
- Use the generated library in your code
- Alternative methods
Install protoc with the gRPC plugin
On Mac OS X, install homebrew. On Linux, install linuxbrew.
Run the following command to install the Protocol Buffers compiler (protoc) and the gRPC protoc plugin:
$ curl -fsSL https://goo.gl/getgrpc | bash -
This will download and run the gRPC install script. After the command completes, you're ready to proceed.
Use protoc to generate a gRPC library
Run protoc with the following flags to generate the client library for your .proto
files:
protoc --objc_out=. --objcgrpc_out=. *.proto
This will generate a pair of .pbobjc.h
/.pbobjc.m
files for each .proto
file, with the messages and enums defined in them. And a pair of .pbrpc.h
/.pbrpc.m
files for each .proto
file with services defined. The latter contains the code to make remote calls to the specified API.
Integrate the generated gRPC library in your project
Install Cocoapods.
You need to create a Podspec file for the generated library. You may simply copy the following example to the directory where the source files were generated, updating the name and other metadata of the Podspec as necessary:
Pod::Spec.new do |s|
s.name = '<Podspec file name>'
s.version = '...'
s.summary = 'Client library to make RPCs to <my proto API>'
s.homepage = '...'
s.license = '...'
s.authors = { '<my name>' => '<my email>' }
s.ios.deployment_target = '6.0'
s.osx.deployment_target = '10.8'
s.subspec 'Messages' do |ms|
ms.source_files = '*.pbobjc.{h,m}'
ms.requires_arc = false
ms.dependency 'Protobuf', '~> 3.0'
end
s.subspec 'Services' do |ss|
ss.source_files = '*.pbrpc.{h,m}'
ss.requires_arc = true
ss.dependency 'gRPC', '~> 0.0'
ss.dependency '<Podspec file name>/Messages'
end
end
The file should be named <Podspec file name>.podspec
. You can refer to this example Podspec. Once your library has a Podspec, Cocoapods can install it into any XCode project. For that, go into your project's directory and create a Podfile by running:
pod init
Next add a line to your Podfile to refer to your library's Podspec. Use :path
as described here:
pod '<Podspec file name>', :path => 'path/to/the/directory/of/your/podspec'
You can look at this example Podfile.
Finally, in your project's directory, run:
pod install
Use the generated library in your code
Please check this sample app for examples of how to use a generated gRPC library.
Alternative methods
Install protoc and the gRPC plugin without using Homebrew
First install v3 of the Protocol Buffers compiler (protoc), by cloning its Git repository and following these installation instructions (the ones titled C++; don't miss the note for Mac users).
Then clone this repository and execute the following commands from the root directory where it was cloned.
Compile the gRPC plugins for protoc:
make plugins
Create a symbolic link to the compiled plugin binary somewhere in your $PATH
:
ln -s `pwd`/bins/opt/grpc_objective_c_plugin /usr/local/bin/protoc-gen-objcgrpc
(Notice that the name of the created link must begin with "protoc-gen-" for protoc to recognize it as a plugin).
If you don't want to create the symbolic link, you can alternatively copy the binary (with the appropriate name). Or you might prefer instead to specify the plugin's path as a flag when invoking protoc, in which case no system modification nor renaming is necessary.
Integrate the generated gRPC library without using Cocoapods
You need to compile the generated .pbpbjc.*
files (the enums and messages) without ARC support, and the generated .pbrpc.*
files (the services) with ARC support. The generated code depends on v0.3+ of the Objective-C gRPC runtime library and v3.0+ of the Objective-C Protobuf runtime library.
These libraries need to be integrated into your project as described in their respective Podspec files: