1[](https://cocoapods.org/pods/gRPC) 2# gRPC for Objective-C 3gRPC Objective C library provides Objective C API for users to make gRPC calls on iOS or OS X 4platforms. Currently, the minimum supported iOS version is 9.0 and OS X version is 10.10 (Yosemite). 5 6While gRPC doesn't require the use of an IDL to describe the API of services, using one simplifies 7usage and adds some interoperability guarantees. Here we use [Protocol Buffers][], and provide a 8plugin for the Protobuf Compiler (_protoc_) to generate client libraries to communicate with gRPC 9services. 10 11- [Write your API declaration in proto format](#write-protos) 12- [Integrate a proto library in your project](#cocoapods) 13- [Use the generated library in your code](#use) 14- [Use gRPC without Protobuf](#no-proto) 15- [Alternatives to the steps above](#alternatives) 16 - [Install protoc with the gRPC plugin](#install) 17 - [Install protoc and the gRPC plugin without using Homebrew](#no-homebrew) 18 - [Integrate the generated gRPC library without using Cocoapods](#no-cocoapods) 19 20<a name="write-protos"></a> 21## Write your API declaration in proto format 22 23For this you can consult the [Protocol Buffers][]' official documentation, or learn from a quick 24example [here](https://github.com/grpc/grpc/tree/master/examples#defining-a-service). 25 26<a name="cocoapods"></a> 27## Integrate a proto library in your project 28 29Install [Cocoapods](https://cocoapods.org/#install). 30 31You need to create a Podspec file for your proto library. You may simply copy the following example 32to the directory where your `.proto` files are located, updating the name, version and license as 33necessary. You also need to set the `pods_root` variable to the correct value, depending on where 34you place this podspec relative to your Podfile. 35 36```ruby 37Pod::Spec.new do |s| 38 s.name = '<Podspec file name>' 39 s.version = '0.0.1' 40 s.license = '...' 41 s.authors = { '<your name>' => '<your email>' } 42 s.homepage = '...' 43 s.summary = '...' 44 s.source = { :git => 'https://github.com/...' } 45 46 s.ios.deployment_target = '10.0' 47 s.osx.deployment_target = '10.12' 48 s.tvos.deployment_target = '12.0' 49 s.watchos.deployment_target = '6.0' 50 51 # Base directory where the .proto files are. 52 src = '.' 53 54 # We'll use protoc with the gRPC plugin. 55 s.dependency '!ProtoCompiler-gRPCPlugin', '~> 1.0' 56 57 # Pods directory corresponding to this app's Podfile, relative to the location of this podspec. 58 pods_root = '<path to your Podfile>/Pods' 59 60 # Path where Cocoapods downloads protoc and the gRPC plugin. 61 protoc_dir = "#{pods_root}/!ProtoCompiler" 62 protoc = "#{protoc_dir}/protoc" 63 plugin = "#{pods_root}/!ProtoCompiler-gRPCPlugin/grpc_objective_c_plugin" 64 65 # Directory where you want the generated files to be placed. This is an example. 66 dir = "#{pods_root}/#{s.name}" 67 68 # Run protoc with the Objective-C and gRPC plugins to generate protocol messages and gRPC clients. 69 # You can run this command manually if you later change your protos and need to regenerate. 70 # Alternatively, you can advance the version of this podspec and run `pod update`. 71 s.prepare_command = <<-CMD 72 mkdir -p #{dir} 73 #{protoc} \ 74 --plugin=protoc-gen-grpc=#{plugin} \ 75 --objc_out=#{dir} \ 76 --grpc_out=#{dir} \ 77 -I #{src} \ 78 -I #{protoc_dir} \ 79 #{src}/*.proto 80 CMD 81 82 # The --objc_out plugin generates a pair of .pbobjc.h/.pbobjc.m files for each .proto file. 83 s.subspec 'Messages' do |ms| 84 ms.source_files = "#{dir}/*.pbobjc.{h,m}" 85 ms.header_mappings_dir = dir 86 ms.requires_arc = false 87 # The generated files depend on the protobuf runtime. 88 ms.dependency 'Protobuf' 89 end 90 91 # The --objcgrpc_out plugin generates a pair of .pbrpc.h/.pbrpc.m files for each .proto file with 92 # a service defined. 93 s.subspec 'Services' do |ss| 94 ss.source_files = "#{dir}/*.pbrpc.{h,m}" 95 ss.header_mappings_dir = dir 96 ss.requires_arc = true 97 # The generated files depend on the gRPC runtime, and on the files generated by `--objc_out`. 98 ss.dependency 'gRPC-ProtoRPC' 99 ss.dependency "#{s.name}/Messages" 100 end 101 102 s.pod_target_xcconfig = { 103 # This is needed by all pods that depend on Protobuf: 104 'GCC_PREPROCESSOR_DEFINITIONS' => '$(inherited) GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS=1', 105 # This is needed by all pods that depend on gRPC-RxLibrary: 106 'CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES' => 'YES', 107 } 108end 109``` 110 111The file should be named `<Podspec file name>.podspec`. 112 113Note: If your proto files are in a directory hierarchy, you might want to adjust the _globs_ used in 114the sample Podspec above. For example, you could use: 115 116```ruby 117 s.prepare_command = <<-CMD 118 ... 119 `find . -name *.proto -print | xargs` 120 CMD 121 ... 122 ms.source_files = "#{dir}/*.pbobjc.{h,m}", "#{dir}/**/*.pbobjc.{h,m}" 123 ... 124 ss.source_files = "#{dir}/*.pbrpc.{h,m}", "#{dir}/**/*.pbrpc.{h,m}" 125``` 126 127Once your library has a Podspec, Cocoapods can install it into any XCode project. For that, go into 128your project's directory and create a Podfile by running: 129 130```sh 131pod init 132``` 133 134Next add a line to your Podfile to refer to your library's Podspec. Use `:path` as described 135[here](https://guides.cocoapods.org/using/the-podfile.html#using-the-files-from-a-folder-local-to-the-machine): 136 137```ruby 138pod '<Podspec file name>', :path => 'path/to/the/directory/of/your/podspec' 139``` 140 141You can look at this [example Podfile][]. 142 143Finally, in your project's directory, run: 144 145```sh 146pod install 147``` 148 149<a name="use"></a> 150## Use the generated library in your code 151 152Please check the [example apps][] for examples of how to use a generated gRPC library. 153 154<a name="no-proto"></a> 155## Use gRPC without Protobuf 156 157This [tests file](https://github.com/grpc/grpc/tree/master/src/objective-c/tests/GRPCClientTests.m) 158shows how to use the generic gRPC Objective-C client without generated protobuf files. 159 160<a name="alternatives"></a> 161## Alternatives to the steps above 162 163<a name="install"></a> 164### Install _protoc_ with the gRPC plugin 165 166Although it's not recommended (because it can lead to hard-to-solve version conflicts), it is 167sometimes more convenient to install _protoc_ and the gRPC plugin in your development machine, 168instead of letting Cocoapods download the appropriate versions for you. To do so, on Mac OS X or 169later, install [homebrew][]. 170 171The run the following command to install _protoc_ and the gRPC _protoc_ plugin: 172```sh 173$ curl -fsSL https://goo.gl/getgrpc | bash - 174``` 175This will download and run the [gRPC install script][]. 176 177<a name="no-homebrew"></a> 178### Install _protoc_ and the gRPC plugin without using Homebrew 179 180First install v3 of the Protocol Buffers compiler (_protoc_), by cloning 181[its Git repository](https://github.com/protocolbuffers/protobuf) and following these 182[installation instructions](https://github.com/protocolbuffers/protobuf#c-installation---unix) 183(the ones titled C++; don't miss the note for Mac users). 184 185Then clone this repository and execute the following commands from the root directory where it was 186cloned. 187 188Compile the gRPC plugins for _protoc_: 189```sh 190make grpc_objective_c_plugin 191``` 192 193Create a symbolic link to the compiled plugin binary somewhere in your `$PATH`: 194```sh 195ln -s `pwd`/bins/opt/grpc_objective_c_plugin /usr/local/bin/protoc-gen-objcgrpc 196``` 197(Notice that the name of the created link must begin with "`protoc-gen-`" for _protoc_ to recognize 198it as a plugin). 199 200If you don't want to create the symbolic link, you can alternatively copy the binary (with the 201appropriate name). Or you might prefer instead to specify the plugin's path as a flag when invoking 202_protoc_, in which case no system modification nor renaming is necessary. 203 204<a name="no-cocoapods"></a> 205### Integrate the generated gRPC library without using Cocoapods 206 207You need to compile the generated `.pbobjc.*` files (the enums and messages) without ARC support, 208and the generated `.pbrpc.*` files (the services) with ARC support. The generated code depends on 209v0.12+ of the Objective-C gRPC runtime library and v3.0.0-alpha-4+ of the Objective-C Protobuf 210runtime library. 211 212These libraries need to be integrated into your project as described in their respective Podspec 213files: 214 215* [Podspec](https://github.com/grpc/grpc/blob/master/gRPC.podspec) for the Objective-C gRPC runtime 216library. This can be tedious to configure manually. 217* [Podspec](https://github.com/protocolbuffers/protobuf/blob/master/Protobuf.podspec) for the 218Objective-C Protobuf runtime library. 219 220[Protocol Buffers]:https://developers.google.com/protocol-buffers/ 221[homebrew]:http://brew.sh 222[gRPC install script]:https://raw.githubusercontent.com/grpc/homebrew-grpc/master/scripts/install 223[example Podfile]:https://github.com/grpc/grpc/blob/master/examples/objective-c/helloworld/Podfile 224[example apps]: https://github.com/grpc/grpc/tree/master/examples/objective-c 225 226## Use gRPC with OpenSSL 227gRPC uses BoringSSL as its dependency, which is a fork of OpenSSL and export a number of symbols 228that are the same as OpenSSL. gRPC avoids conflicts of these symbols by renaming BoringSSL symbols. 229 230If you need gRPC to use OpenSSL instead of BoringSSL (e.g. for the benefit of reducing the binary 231size of your product), you need to make a local `gRPC-Core` podspec and tweak it accordingly: 232- Copy the version of `/gRPC-Core.podspec` you wish to use from Github into the repository of your 233 app; 234- In your `Podfile`, add the following line: 235``` 236pod `gRPC-Core`, :podspec => "." # assuming gRPC-Core.podspec is in the same directory as your Podfile 237``` 238- Remove [the 239 macro](https://github.com/grpc/grpc/blob/b24b212ee585d376c618235905757b2445ac6461/gRPC-Core.podspec#L186) 240 `GRPC_SHADOW_BORINGSSL_SYMBOLS` to disable symbol renaming; 241- Substitude the `BoringSSL-GRPC` 242 [dependency](https://github.com/grpc/grpc/blob/b24b212ee585d376c618235905757b2445ac6461/gRPC-Core.podspec#L184) 243 to whatever pod of OpenSSL your other libraries use. 244 245These steps should allow gRPC to use OpenSSL and drop BoringSSL dependency. If you see any issue, 246file an issue to us. 247 248## Upgrade issue with BoringSSL 249If you were using an old version of gRPC (<= v1.14) which depended on pod `BoringSSL` rather than 250`BoringSSL-GRPC` and meet issue with the library like: 251``` 252ld: framework not found openssl 253``` 254updating `-framework openssl` in Other Linker Flags to `-framework openssl_grpc` in your project 255may resolve this issue (see [#16821](https://github.com/grpc/grpc/issues/16821)). 256