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