diff options
Diffstat (limited to 'src/objective-c')
-rw-r--r-- | src/objective-c/ProtoRPC/ProtoRPC.m | 4 | ||||
-rw-r--r-- | src/objective-c/README.md | 119 | ||||
-rw-r--r-- | src/objective-c/examples/Sample/Podfile | 2 | ||||
-rw-r--r-- | src/objective-c/examples/Sample/Sample/ViewController.m | 2 | ||||
-rw-r--r-- | src/objective-c/examples/Sample/SampleTests/RemoteTests.m | 2 | ||||
-rw-r--r-- | src/objective-c/examples/Sample/SampleTests/SampleTests.m | 2 |
6 files changed, 107 insertions, 24 deletions
diff --git a/src/objective-c/ProtoRPC/ProtoRPC.m b/src/objective-c/ProtoRPC/ProtoRPC.m index 96608f2898..b8f8008422 100644 --- a/src/objective-c/ProtoRPC/ProtoRPC.m +++ b/src/objective-c/ProtoRPC/ProtoRPC.m @@ -59,7 +59,7 @@ responseClass:(Class)responseClass responsesWriteable:(id<GRXWriteable>)responsesWriteable { // Because we can't tell the type system to constrain the class, we need to check at runtime: - if (![responseClass respondsToSelector:@selector(parseFromData:)]) { + if (![responseClass respondsToSelector:@selector(parseFromData:error:)]) { [NSException raise:NSInvalidArgumentException format:@"A protobuf class to parse the responses must be provided."]; } @@ -71,7 +71,7 @@ if ((self = [super initWithHost:host method:method requestsWriter:bytesWriter])) { // A writeable that parses the proto messages received. _responseWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { - [responsesWriteable writeValue:[responseClass parseFromData:value]]; + [responsesWriteable writeValue:[responseClass parseFromData:value error:NULL]]; } completionHandler:^(NSError *errorOrNil) { [responsesWriteable writesFinishedWithError:errorOrNil]; }]; diff --git a/src/objective-c/README.md b/src/objective-c/README.md index cdba3777dd..167016cc2c 100644 --- a/src/objective-c/README.md +++ b/src/objective-c/README.md @@ -1,43 +1,119 @@ # gRPC for Objective-C -## How to generate a client library from a Protocol Buffers definition +- [Install protoc with the gRPC plugin](#install) +- [Use protoc to generate a gRPC library](#protoc) +- [Integrate the generated gRPC library in your project](#cocoapods) +- [Use the generated library in your code](#use) +- [Alternative methods](#alternatives) + - [Install protoc and the gRPC plugin without using Homebrew](#nohomebrew) + - [Integrate the generated gRPC library without using Cocoapods](#nococoapods) -First install v3 of the Protocol Buffers compiler (_protoc_), by cloning [its Git repository](https://github.com/google/protobuf) and following these [installation instructions](https://github.com/google/protobuf#c-installation---unix) (the ones titled C++; don't miss the note for Mac users). +<a name="install"></a> +## Install protoc with the gRPC plugin -Then clone this repository and execute the following commands from the root directory where it was cloned. +On Mac OS X, install [homebrew][]. On Linux, install [linuxbrew][]. -Compile the gRPC plugins for _protoc_: +Run the following command to install the Protocol Buffers compiler (_protoc_) and the gRPC _protoc_ plugin: ```sh -make plugins +$ 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. + +<a name="protoc"></a> +## Use protoc to generate a gRPC library + +Run _protoc_ with the following flags to generate the client library for your `.proto` files: -Create a symbolic link to the compiled plugin binary somewhere in your `$PATH`: ```sh -ln -s `pwd`/bins/opt/grpc_objective_c_plugin /usr/local/bin/protoc-gen-objcgrpc +protoc --objc_out=. --objcgrpc_out=. *.proto ``` -(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. +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. + +<a name="cocoapods"></a> +## Integrate the generated gRPC library in your project + +Install [Cocoapods](https://cocoapods.org/#install). + +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: + +```ruby +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 +``` -Finally, run _protoc_ with the following flags to generate the client library for your `.proto` files: +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: ```sh -protoc --objc_out=. --objcrpc_out=. *.proto +pod init ``` -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. +Next add a line to your Podfile to refer to your library's Podspec. Use `:path` as described [here](https://guides.cocoapods.org/using/the-podfile.html#using-the-files-from-a-folder-local-to-the-machine): + +```ruby +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: + +```sh +pod install +``` -## How to integrate a generated gRPC library in your project +<a name="use"></a> +## Use the generated library in your code -### If you use Cocoapods +Please check this [sample app][] for examples of how to use a generated gRPC library. -This is the recommended approach. +<a name="alternatives"></a> +## Alternative methods -You need to create a Podspec file for the generated library. This is simply a matter of copying an example like [this one](https://github.com/grpc/grpc/blob/master/src/objective-c/examples/Sample/RemoteTestClient/RemoteTest.podspec) to the directory where the source files were generated. Update the name and other metadata of the Podspec as suitable. +<a name="nohomebrew"></a> +### Install protoc and the gRPC plugin without using Homebrew -Once your library has a Podspec, refer to it from your Podfile using `:path` as described [here](https://guides.cocoapods.org/using/the-podfile.html#using-the-files-from-a-folder-local-to-the-machine). +First install v3 of the Protocol Buffers compiler (_protoc_), by cloning [its Git repository](https://github.com/google/protobuf) and following these [installation instructions](https://github.com/google/protobuf#c-installation---unix) (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. -### If you don't use Cocoapods +Compile the gRPC plugins for _protoc_: +```sh +make plugins +``` + +Create a symbolic link to the compiled plugin binary somewhere in your `$PATH`: +```sh +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. + +<a name="nococoapods"></a> +### 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. @@ -45,3 +121,10 @@ These libraries need to be integrated into your project as described in their re * [Podspec](https://github.com/grpc/grpc/blob/master/gRPC.podspec) for the Objective-C gRPC runtime library. This can be tedious to configure manually. * [Podspec](https://github.com/jcanizales/protobuf/blob/add-podspec/Protobuf.podspec) for the Objective-C Protobuf runtime library. + +[homebrew]:http://brew.sh +[linuxbrew]:https://github.com/Homebrew/linuxbrew +[gRPC install script]:https://raw.githubusercontent.com/grpc/homebrew-grpc/master/scripts/install +[example Podspec]:https://github.com/grpc/grpc/blob/master/src/objective-c/examples/Sample/RemoteTestClient/RemoteTest.podspec +[example Podfile]:https://github.com/grpc/grpc/blob/master/src/objective-c/examples/Sample/Podfile +[sample app]: https://github.com/grpc/grpc/tree/master/src/objective-c/examples/Sample diff --git a/src/objective-c/examples/Sample/Podfile b/src/objective-c/examples/Sample/Podfile index d30d9c5210..e8b78647ac 100644 --- a/src/objective-c/examples/Sample/Podfile +++ b/src/objective-c/examples/Sample/Podfile @@ -2,7 +2,7 @@ source 'https://github.com/CocoaPods/Specs.git' platform :ios, '8.0' pod 'gRPC', :path => "../../../.." -pod 'Protobuf', :git => 'https://github.com/jcanizales/protobuf.git', :branch => 'add-podspec' +pod 'Protobuf', :git => 'https://github.com/google/protobuf.git' pod 'Route_guide', :path => "RouteGuideClient" pod 'RemoteTest', :path => "RemoteTestClient" diff --git a/src/objective-c/examples/Sample/Sample/ViewController.m b/src/objective-c/examples/Sample/Sample/ViewController.m index 1c866babec..9b331fe43f 100644 --- a/src/objective-c/examples/Sample/Sample/ViewController.m +++ b/src/objective-c/examples/Sample/Sample/ViewController.m @@ -80,7 +80,7 @@ requestsWriter:requestsWriter]; id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { - RMTSimpleResponse *response = [RMTSimpleResponse parseFromData:value]; + RMTSimpleResponse *response = [RMTSimpleResponse parseFromData:value error:NULL]; NSLog(@"Received response:\n%@", response); } completionHandler:^(NSError *errorOrNil) { if (errorOrNil) { diff --git a/src/objective-c/examples/Sample/SampleTests/RemoteTests.m b/src/objective-c/examples/Sample/SampleTests/RemoteTests.m index 553c813b0b..ceb72cfaec 100644 --- a/src/objective-c/examples/Sample/SampleTests/RemoteTests.m +++ b/src/objective-c/examples/Sample/SampleTests/RemoteTests.m @@ -125,7 +125,7 @@ XCTAssertNotNil(value, @"nil value received as response."); [response fulfill]; XCTAssertGreaterThan(value.length, 0, @"Empty response received."); - RMTSimpleResponse *response = [RMTSimpleResponse parseFromData:value]; + RMTSimpleResponse *response = [RMTSimpleResponse parseFromData:value error:NULL]; // We expect empty strings, not nil: XCTAssertNotNil(response.username, @"Response's username is nil."); XCTAssertNotNil(response.oauthScope, @"Response's OAuth scope is nil."); diff --git a/src/objective-c/examples/Sample/SampleTests/SampleTests.m b/src/objective-c/examples/Sample/SampleTests/SampleTests.m index b1a0f78a01..83cfd8c1b5 100644 --- a/src/objective-c/examples/Sample/SampleTests/SampleTests.m +++ b/src/objective-c/examples/Sample/SampleTests/SampleTests.m @@ -123,7 +123,7 @@ id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) { XCTAssertNotNil(value, @"nil value received as response."); - RGDFeature *feature = [RGDFeature parseFromData:value]; + RGDFeature *feature = [RGDFeature parseFromData:value error:NULL]; XCTAssertEqualObjects(point, feature.location); XCTAssertNotNil(feature.name, @"Response's name is nil."); [response fulfill]; |