1*cc02d7e2SAndroid Build Coastguard Worker# gRPC in 3 minutes (Objective-C) 2*cc02d7e2SAndroid Build Coastguard Worker 3*cc02d7e2SAndroid Build Coastguard WorkerThere are currently two ways to build projects with the gRPC Objective-C library: 4*cc02d7e2SAndroid Build Coastguard Worker* Cocoapods & Xcode 5*cc02d7e2SAndroid Build Coastguard Worker* Bazel (experimental) 6*cc02d7e2SAndroid Build Coastguard Worker 7*cc02d7e2SAndroid Build Coastguard Worker## Cocoapods 8*cc02d7e2SAndroid Build Coastguard Worker 9*cc02d7e2SAndroid Build Coastguard Worker## Installation 10*cc02d7e2SAndroid Build Coastguard Worker 11*cc02d7e2SAndroid Build Coastguard WorkerTo run this example you should have [Cocoapods](https://cocoapods.org/#install) installed, as well 12*cc02d7e2SAndroid Build Coastguard Workeras the relevant tools to generate the client library code (and a server in another language, for 13*cc02d7e2SAndroid Build Coastguard Workertesting). You can obtain the latter by following [these setup instructions](https://github.com/grpc/homebrew-grpc). 14*cc02d7e2SAndroid Build Coastguard Worker 15*cc02d7e2SAndroid Build Coastguard Worker### Hello Objective-C gRPC! 16*cc02d7e2SAndroid Build Coastguard Worker 17*cc02d7e2SAndroid Build Coastguard WorkerHere's how to build and run the Objective-C implementation of the [Hello World](../../protos/helloworld.proto) 18*cc02d7e2SAndroid Build Coastguard Workerexample used in [Getting started](https://github.com/grpc/grpc/tree/master/examples). 19*cc02d7e2SAndroid Build Coastguard Worker 20*cc02d7e2SAndroid Build Coastguard WorkerThe example code for this and our other examples lives in the `examples` directory. Clone 21*cc02d7e2SAndroid Build Coastguard Workerthis repository at the [latest stable release tag](https://github.com/grpc/grpc/releases) to your local machine by running the following commands: 22*cc02d7e2SAndroid Build Coastguard Worker 23*cc02d7e2SAndroid Build Coastguard Worker 24*cc02d7e2SAndroid Build Coastguard Worker```sh 25*cc02d7e2SAndroid Build Coastguard Worker$ git clone -b RELEASE_TAG_HERE https://github.com/grpc/grpc 26*cc02d7e2SAndroid Build Coastguard Worker$ cd grpc 27*cc02d7e2SAndroid Build Coastguard Worker$ git submodule update --init 28*cc02d7e2SAndroid Build Coastguard Worker``` 29*cc02d7e2SAndroid Build Coastguard Worker 30*cc02d7e2SAndroid Build Coastguard WorkerChange your current directory to `examples/objective-c/helloworld` 31*cc02d7e2SAndroid Build Coastguard Worker 32*cc02d7e2SAndroid Build Coastguard Worker```sh 33*cc02d7e2SAndroid Build Coastguard Worker$ cd examples/objective-c/helloworld 34*cc02d7e2SAndroid Build Coastguard Worker``` 35*cc02d7e2SAndroid Build Coastguard Worker 36*cc02d7e2SAndroid Build Coastguard Worker#### Try it! 37*cc02d7e2SAndroid Build Coastguard WorkerTo try the sample app, we need a gRPC server running locally. Let's compile and run, for example, 38*cc02d7e2SAndroid Build Coastguard Workerthe C++ server in this repository: 39*cc02d7e2SAndroid Build Coastguard Worker 40*cc02d7e2SAndroid Build Coastguard Worker```shell 41*cc02d7e2SAndroid Build Coastguard Worker$ pushd ../../cpp/helloworld 42*cc02d7e2SAndroid Build Coastguard Worker$ make 43*cc02d7e2SAndroid Build Coastguard Worker$ ./greeter_server & 44*cc02d7e2SAndroid Build Coastguard Worker$ popd 45*cc02d7e2SAndroid Build Coastguard Worker``` 46*cc02d7e2SAndroid Build Coastguard Worker 47*cc02d7e2SAndroid Build Coastguard WorkerNow have Cocoapods generate and install the client library for our .proto files: 48*cc02d7e2SAndroid Build Coastguard Worker 49*cc02d7e2SAndroid Build Coastguard Worker```shell 50*cc02d7e2SAndroid Build Coastguard Worker$ pod install 51*cc02d7e2SAndroid Build Coastguard Worker``` 52*cc02d7e2SAndroid Build Coastguard Worker 53*cc02d7e2SAndroid Build Coastguard Worker(This might have to compile OpenSSL, which takes around 15 minutes if Cocoapods doesn't have it yet 54*cc02d7e2SAndroid Build Coastguard Workeron your computer's cache.) 55*cc02d7e2SAndroid Build Coastguard Worker 56*cc02d7e2SAndroid Build Coastguard WorkerFinally, open the XCode workspace created by Cocoapods, and run the app. You can check the calling 57*cc02d7e2SAndroid Build Coastguard Workercode in `main.m` and see the results in XCode's log console. 58*cc02d7e2SAndroid Build Coastguard Worker 59*cc02d7e2SAndroid Build Coastguard WorkerThe code sends a `HLWHelloRequest` containing the string "Objective-C" to a local server. The server 60*cc02d7e2SAndroid Build Coastguard Workerresponds with a `HLWHelloResponse`, which contains a string that is then output to the log. 61*cc02d7e2SAndroid Build Coastguard Worker 62*cc02d7e2SAndroid Build Coastguard Worker## Bazel 63*cc02d7e2SAndroid Build Coastguard Worker### Installation 64*cc02d7e2SAndroid Build Coastguard WorkerTo run the examples in Bazel, you should have [Bazel](https://docs.bazel.build/versions/master/install-os-x.html) installed. 65*cc02d7e2SAndroid Build Coastguard Worker 66*cc02d7e2SAndroid Build Coastguard Worker### Hello Objective-C gRPC! 67*cc02d7e2SAndroid Build Coastguard WorkerHere's how to build and run the Objective-C implementation of the [Hello World](helloworld) example. 68*cc02d7e2SAndroid Build Coastguard Worker 69*cc02d7e2SAndroid Build Coastguard WorkerThe code for the Hello World example and others live in the `examples` directory. Clone this repository to your local machine by running the following commands: 70*cc02d7e2SAndroid Build Coastguard Worker```shell 71*cc02d7e2SAndroid Build Coastguard Worker$ git clone --recursive https://github.com/grpc/grpc 72*cc02d7e2SAndroid Build Coastguard Worker``` 73*cc02d7e2SAndroid Build Coastguard Worker 74*cc02d7e2SAndroid Build Coastguard WorkerNext, change your directory to `examples/objective-c` 75*cc02d7e2SAndroid Build Coastguard Worker```shell 76*cc02d7e2SAndroid Build Coastguard Worker$ cd grpc/examples/objective-c 77*cc02d7e2SAndroid Build Coastguard Worker``` 78*cc02d7e2SAndroid Build Coastguard Worker 79*cc02d7e2SAndroid Build Coastguard WorkerNow build the Hello World project: 80*cc02d7e2SAndroid Build Coastguard Worker```shell 81*cc02d7e2SAndroid Build Coastguard Worker$ bazel build :HelloWorld 82*cc02d7e2SAndroid Build Coastguard Worker``` 83*cc02d7e2SAndroid Build Coastguard Worker 84*cc02d7e2SAndroid Build Coastguard Worker#### Try it! 85*cc02d7e2SAndroid Build Coastguard WorkerTo run the Hello World sample properly, we need a local server. Let's compile and run the corresponding C++ server: 86*cc02d7e2SAndroid Build Coastguard Worker```shell 87*cc02d7e2SAndroid Build Coastguard Worker$ bazel run //examples/cpp/helloworld:greeter_server 88*cc02d7e2SAndroid Build Coastguard Worker``` 89*cc02d7e2SAndroid Build Coastguard Worker 90*cc02d7e2SAndroid Build Coastguard WorkerTo run the sample, you need to know the available simulator runtimes in your machine. You could either list the available runtimes yourself by running: 91*cc02d7e2SAndroid Build Coastguard Worker```shell 92*cc02d7e2SAndroid Build Coastguard Worker$ xcrun simctl list 93*cc02d7e2SAndroid Build Coastguard Worker``` 94*cc02d7e2SAndroid Build Coastguard WorkerOr just try running the app and it will let you know what is available in the error messages: 95*cc02d7e2SAndroid Build Coastguard Worker```shell 96*cc02d7e2SAndroid Build Coastguard Worker$ bazel run :HelloWorld 97*cc02d7e2SAndroid Build Coastguard Worker``` 98*cc02d7e2SAndroid Build Coastguard WorkerNote that running this command will build the project even if it is not built beforehand. 99*cc02d7e2SAndroid Build Coastguard Worker 100*cc02d7e2SAndroid Build Coastguard WorkerFinally, launch the app with one of the available runtimes: 101*cc02d7e2SAndroid Build Coastguard Worker```shell 102*cc02d7e2SAndroid Build Coastguard Worker$ bazel run :HelloWorld --ios_simulator_version='<runtime>' --ios_sumlator_device='<device>' 103*cc02d7e2SAndroid Build Coastguard Worker``` 104*cc02d7e2SAndroid Build Coastguard Worker 105*cc02d7e2SAndroid Build Coastguard Worker## Tutorial 106*cc02d7e2SAndroid Build Coastguard Worker 107*cc02d7e2SAndroid Build Coastguard WorkerYou can find a more detailed tutorial in [gRPC Basics: Objective-C](https://grpc.io/docs/languages/objective-c/basics). 108