OLD | NEW |
(Empty) | |
| 1 [](https://cocoapods.or
g/pods/gRPC) |
| 2 # gRPC for Objective-C |
| 3 |
| 4 - [Install protoc with the gRPC plugin](#install) |
| 5 - [Write your API declaration in proto format](#write-protos) |
| 6 - [Integrate a proto library in your project](#cocoapods) |
| 7 - [Use the generated library in your code](#use) |
| 8 - [Use gRPC without Protobuf](#no-proto) |
| 9 - [Alternative installation methods](#alternatives) |
| 10 - [Install protoc and the gRPC plugin without using Homebrew](#no-homebrew) |
| 11 - [Integrate the generated gRPC library without using Cocoapods](#no-cocoapo
ds) |
| 12 |
| 13 While gRPC doesn't require the use of an IDL to describe the API of services, us
ing one simplifies |
| 14 usage and adds some interoperability guarantees. Here we use [Protocol Buffers][
], and provide a |
| 15 plugin for the Protobuf Compiler (_protoc_) to generate client libraries to comm
unicate with gRPC |
| 16 services. |
| 17 |
| 18 <a name="install"></a> |
| 19 ## Install protoc with the gRPC plugin |
| 20 |
| 21 On Mac OS X, install [homebrew][]. |
| 22 |
| 23 Run the following command to install _protoc_ and the gRPC _protoc_ plugin: |
| 24 ```sh |
| 25 $ curl -fsSL https://goo.gl/getgrpc | bash - |
| 26 ``` |
| 27 This will download and run the [gRPC install script][]. After the command comple
tes, you're ready to |
| 28 proceed. |
| 29 |
| 30 <a name="write-protos"></a> |
| 31 ## Write your API declaration in proto format |
| 32 |
| 33 For this you can consult the [Protocol Buffers][]' official documentation, or le
arn from a quick |
| 34 example [here](https://github.com/grpc/grpc/tree/master/examples#defining-a-serv
ice). |
| 35 |
| 36 <a name="cocoapods"></a> |
| 37 ## Integrate a proto library in your project |
| 38 |
| 39 Install [Cocoapods](https://cocoapods.org/#install). |
| 40 |
| 41 You need to create a Podspec file for your proto library. You may simply copy th
e following example |
| 42 to the directory where your `.proto` files are located, updating the name, versi
on and license as |
| 43 necessary: |
| 44 |
| 45 ```ruby |
| 46 Pod::Spec.new do |s| |
| 47 s.name = '<Podspec file name>' |
| 48 s.version = '0.0.1' |
| 49 s.license = '...' |
| 50 |
| 51 s.ios.deployment_target = '7.1' |
| 52 s.osx.deployment_target = '10.9' |
| 53 |
| 54 # Run protoc with the Objective-C and gRPC plugins to generate protocol messag
es and gRPC clients. |
| 55 # You can run this command manually if you later change your protos and need t
o regenerate. |
| 56 s.prepare_command = "protoc --objc_out=. --objcgrpc_out=. *.proto" |
| 57 |
| 58 # The --objc_out plugin generates a pair of .pbobjc.h/.pbobjc.m files for each
.proto file. |
| 59 s.subspec "Messages" do |ms| |
| 60 ms.source_files = "*.pbobjc.{h,m}" |
| 61 ms.header_mappings_dir = "." |
| 62 ms.requires_arc = false |
| 63 ms.dependency "Protobuf", "~> 3.0.0-alpha-4" |
| 64 end |
| 65 |
| 66 # The --objcgrpc_out plugin generates a pair of .pbrpc.h/.pbrpc.m files for ea
ch .proto file with |
| 67 # a service defined. |
| 68 s.subspec "Services" do |ss| |
| 69 ss.source_files = "*.pbrpc.{h,m}" |
| 70 ss.header_mappings_dir = "." |
| 71 ss.requires_arc = true |
| 72 ss.dependency "gRPC", "~> 0.12" |
| 73 ss.dependency "#{s.name}/Messages" |
| 74 end |
| 75 end |
| 76 ``` |
| 77 |
| 78 The file should be named `<Podspec file name>.podspec`. |
| 79 |
| 80 Note: If your proto files are in a directory hierarchy, you might want to adjust
the _globs_ used in |
| 81 the sample Podspec above. For example, you could use: |
| 82 |
| 83 ```ruby |
| 84 s.prepare_command = "protoc --objc_out=. --objcgrpc_out=. *.proto **/*.proto" |
| 85 ... |
| 86 ms.source_files = "*.pbobjc.{h,m}", "**/*.pbobjc.{h,m}" |
| 87 ... |
| 88 ss.source_files = "*.pbrpc.{h,m}", "**/*.pbrpc.{h,m}" |
| 89 ``` |
| 90 |
| 91 Once your library has a Podspec, Cocoapods can install it into any XCode project
. For that, go into |
| 92 your project's directory and create a Podfile by running: |
| 93 |
| 94 ```sh |
| 95 pod init |
| 96 ``` |
| 97 |
| 98 Next add a line to your Podfile to refer to your library's Podspec. Use `:path`
as described |
| 99 [here](https://guides.cocoapods.org/using/the-podfile.html#using-the-files-from-
a-folder-local-to-the-machine): |
| 100 |
| 101 ```ruby |
| 102 pod '<Podspec file name>', :path => 'path/to/the/directory/of/your/podspec' |
| 103 ``` |
| 104 |
| 105 You can look at this [example Podfile][]. |
| 106 |
| 107 Finally, in your project's directory, run: |
| 108 |
| 109 ```sh |
| 110 pod install |
| 111 ``` |
| 112 |
| 113 <a name="use"></a> |
| 114 ## Use the generated library in your code |
| 115 |
| 116 Please check this [sample app][] for examples of how to use a generated gRPC lib
rary. |
| 117 |
| 118 <a name="no-proto"></a> |
| 119 ## Use gRPC without Protobuf |
| 120 |
| 121 The [sample app][] has an example of how to use the generic gRPC Objective-C cli
ent without |
| 122 generated files. |
| 123 |
| 124 <a name="alternatives"></a> |
| 125 ## Alternative installation methods |
| 126 |
| 127 <a name="no-homebrew"></a> |
| 128 ### Install protoc and the gRPC plugin without using Homebrew |
| 129 |
| 130 First install v3 of the Protocol Buffers compiler (_protoc_), by cloning |
| 131 [its Git repository](https://github.com/google/protobuf) and following these |
| 132 [installation instructions](https://github.com/google/protobuf#c-installation---
unix) |
| 133 (the ones titled C++; don't miss the note for Mac users). |
| 134 |
| 135 Then clone this repository and execute the following commands from the root dire
ctory where it was |
| 136 cloned. |
| 137 |
| 138 Compile the gRPC plugins for _protoc_: |
| 139 ```sh |
| 140 make plugins |
| 141 ``` |
| 142 |
| 143 Create a symbolic link to the compiled plugin binary somewhere in your `$PATH`: |
| 144 ```sh |
| 145 ln -s `pwd`/bins/opt/grpc_objective_c_plugin /usr/local/bin/protoc-gen-objcgrpc |
| 146 ``` |
| 147 (Notice that the name of the created link must begin with "protoc-gen-" for _pro
toc_ to recognize it |
| 148 as a plugin). |
| 149 |
| 150 If you don't want to create the symbolic link, you can alternatively copy the bi
nary (with the |
| 151 appropriate name). Or you might prefer instead to specify the plugin's path as a
flag when invoking |
| 152 _protoc_, in which case no system modification nor renaming is necessary. |
| 153 |
| 154 <a name="no-cocoapods"></a> |
| 155 ### Integrate the generated gRPC library without using Cocoapods |
| 156 |
| 157 You need to compile the generated `.pbobjc.*` files (the enums and messages) wit
hout ARC support, |
| 158 and the generated `.pbrpc.*` files (the services) with ARC support. The generate
d code depends on |
| 159 v0.12+ of the Objective-C gRPC runtime library and v3.0.0-alpha-4+ of the Object
ive-C Protobuf |
| 160 runtime library. |
| 161 |
| 162 These libraries need to be integrated into your project as described in their re
spective Podspec |
| 163 files: |
| 164 |
| 165 * [Podspec](https://github.com/grpc/grpc/blob/master/gRPC.podspec) for the Objec
tive-C gRPC runtime |
| 166 library. This can be tedious to configure manually. |
| 167 * [Podspec](https://github.com/google/protobuf/blob/master/Protobuf.podspec) for
the |
| 168 Objective-C Protobuf runtime library. |
| 169 |
| 170 [Protocol Buffers]:https://developers.google.com/protocol-buffers/ |
| 171 [homebrew]:http://brew.sh |
| 172 [gRPC install script]:https://raw.githubusercontent.com/grpc/homebrew-grpc/maste
r/scripts/install |
| 173 [example Podfile]:https://github.com/grpc/grpc/blob/master/src/objective-c/examp
les/Sample/Podfile |
| 174 [sample app]: https://github.com/grpc/grpc/tree/master/src/objective-c/examples/
Sample |
OLD | NEW |