1
1
[ ![ Cocoapods] ( https://img.shields.io/cocoapods/v/gRPC.svg )] ( https://cocoapods.org/pods/gRPC )
2
2
# gRPC for Objective-C
3
3
4
- - [ Install protoc with the gRPC plugin] ( #install )
5
4
- [ Write your API declaration in proto format] ( #write-protos )
6
5
- [ Integrate a proto library in your project] ( #cocoapods )
7
6
- [ Use the generated library in your code] ( #use )
8
7
- [ Use gRPC without Protobuf] ( #no-proto )
9
- - [ Alternative installation methods] ( #alternatives )
8
+ - [ Alternatives to the steps above] ( #alternatives )
9
+ - [ Install protoc with the gRPC plugin] ( #install )
10
10
- [ Install protoc and the gRPC plugin without using Homebrew] ( #no-homebrew )
11
11
- [ Integrate the generated gRPC library without using Cocoapods] ( #no-cocoapods )
12
12
@@ -15,18 +15,6 @@ usage and adds some interoperability guarantees. Here we use [Protocol Buffers][
15
15
plugin for the Protobuf Compiler (_ protoc_ ) to generate client libraries to communicate with gRPC
16
16
services.
17
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 completes, you're ready to
28
- proceed.
29
-
30
18
<a name =" write-protos " ></a >
31
19
## Write your API declaration in proto format
32
20
@@ -40,7 +28,8 @@ Install [Cocoapods](https://cocoapods.org/#install).
40
28
41
29
You need to create a Podspec file for your proto library. You may simply copy the following example
42
30
to the directory where your ` .proto ` files are located, updating the name, version and license as
43
- necessary:
31
+ necessary. You also need to set the ` pods_root ` variable to the correct value, depending on where
32
+ you place this podspec relative to your Podfile.
44
33
45
34
``` ruby
46
35
Pod ::Spec .new do |s |
@@ -55,16 +44,44 @@ Pod::Spec.new do |s|
55
44
s.ios.deployment_target = ' 7.1'
56
45
s.osx.deployment_target = ' 10.9'
57
46
47
+ # Base directory where the .proto files are.
48
+ src = ' .'
49
+
50
+ # We'll use protoc with the gRPC plugin.
51
+ s.dependency ' !ProtoCompiler-gRPCPlugin' , ' ~> 0.14'
52
+
53
+ # Pods directory corresponding to this app's Podfile, relative to the location of this podspec.
54
+ pods_root = ' <path to your Podfile>/Pods'
55
+
56
+ # Path where Cocoapods downloads protoc and the gRPC plugin.
57
+ protoc_dir = " #{ pods_root } /!ProtoCompiler"
58
+ protoc = " #{ protoc_dir } /protoc"
59
+ plugin = " #{ pods_root } /!ProtoCompiler-gRPCPlugin/grpc_objective_c_plugin"
60
+
61
+ # Directory where you want the generated files to be placed. This is an example.
62
+ dir = " #{ pods_root } /#{ s.name } "
63
+
58
64
# Run protoc with the Objective-C and gRPC plugins to generate protocol messages and gRPC clients.
59
65
# You can run this command manually if you later change your protos and need to regenerate.
60
- s.prepare_command = " protoc --objc_out=. --objcgrpc_out=. *.proto"
66
+ # Alternatively, you can advance the version of this podspec and run `pod update`.
67
+ s.prepare_command = <<-CMD
68
+ mkdir -p #{ dir }
69
+ #{ protoc } \
70
+ --plugin=protoc-gen-grpc=#{ plugin } \
71
+ --objc_out=#{ dir } \
72
+ --grpc_out=#{ dir } \
73
+ -I #{ src } \
74
+ -I #{ protoc_dir } \
75
+ #{ src } /*.proto
76
+ CMD
61
77
62
78
# The --objc_out plugin generates a pair of .pbobjc.h/.pbobjc.m files for each .proto file.
63
- s.subspec " Messages" do |ms |
64
- ms.source_files = " *.pbobjc.{h,m}"
65
- ms.header_mappings_dir = " . "
79
+ s.subspec ' Messages' do |ms |
80
+ ms.source_files = " #{ dir } / *.pbobjc.{h,m}"
81
+ ms.header_mappings_dir = dir
66
82
ms.requires_arc = false
67
- ms.dependency " Protobuf" , " ~> 3.0.0-beta-2"
83
+ # The generated files depend on the protobuf runtime.
84
+ ms.dependency ' Protobuf'
68
85
# This is needed by all pods that depend on Protobuf:
69
86
ms.pod_target_xcconfig = {
70
87
' GCC_PREPROCESSOR_DEFINITIONS' => ' $(inherited) GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS=1' ,
@@ -73,11 +90,12 @@ Pod::Spec.new do |s|
73
90
74
91
# The --objcgrpc_out plugin generates a pair of .pbrpc.h/.pbrpc.m files for each .proto file with
75
92
# a service defined.
76
- s.subspec " Services" do |ss |
77
- ss.source_files = " *.pbrpc.{h,m}"
78
- ss.header_mappings_dir = " . "
93
+ s.subspec ' Services' do |ss |
94
+ ss.source_files = " #{ dir } / *.pbrpc.{h,m}"
95
+ ss.header_mappings_dir = dir
79
96
ss.requires_arc = true
80
- ss.dependency " gRPC-ProtoRPC" , " ~> 0.14"
97
+ # The generated files depend on the gRPC runtime, and on the files generated by `--objc_out`.
98
+ ss.dependency ' gRPC-ProtoRPC'
81
99
ss.dependency " #{ s.name } /Messages"
82
100
end
83
101
end
@@ -89,11 +107,14 @@ Note: If your proto files are in a directory hierarchy, you might want to adjust
89
107
the sample Podspec above. For example, you could use:
90
108
91
109
``` ruby
92
- s.prepare_command = " protoc --objc_out=. --objcgrpc_out=. *.proto **/*.proto"
110
+ s.prepare_command = <<-CMD
111
+ ...
112
+ #{ src } /*.proto #{ src } /**/*.proto
113
+ CMD
93
114
...
94
- ms.source_files = " *.pbobjc.{h,m}" , " **/*.pbobjc.{h,m}"
115
+ ms.source_files = " #{ dir } / *.pbobjc.{h,m}" , " #{ dir } / **/*.pbobjc.{h,m}"
95
116
...
96
- ss.source_files = " *.pbrpc.{h,m}" , " **/*.pbrpc.{h,m}"
117
+ ss.source_files = " #{ dir } / *.pbrpc.{h,m}" , " #{ dir } / **/*.pbrpc.{h,m}"
97
118
```
98
119
99
120
Once your library has a Podspec, Cocoapods can install it into any XCode project. For that, go into
@@ -121,19 +142,33 @@ pod install
121
142
<a name =" use " ></a >
122
143
## Use the generated library in your code
123
144
124
- Please check this [ sample app ] [ ] for examples of how to use a generated gRPC library.
145
+ Please check the [ example apps ] [ ] for examples of how to use a generated gRPC library.
125
146
126
147
<a name =" no-proto " ></a >
127
148
## Use gRPC without Protobuf
128
149
129
- The [ sample app ] [ ] has an example of how to use the generic gRPC Objective-C client without
130
- generated files.
150
+ This [ tests file ] ( https://github.com/grpc/grpc/tree/master/src/objective-c/tests/GRPCClientTests.m )
151
+ shows how to use the generic gRPC Objective-C client without generated protobuf files.
131
152
132
153
<a name =" alternatives " ></a >
133
- ## Alternative installation methods
154
+ ## Alternatives to the steps above
155
+
156
+ <a name =" install " ></a >
157
+ ### Install _ protoc_ with the gRPC plugin
158
+
159
+ Although it's not recommended (because it can lead to hard-to-solve version conflicts), it is
160
+ sometimes more convenient to install _ protoc_ and the gRPC plugin in your development machine,
161
+ instead of letting Cocoapods download the appropriate versions for you. To do so, on Mac OS X or
162
+ later, install [ homebrew] [ ] .
163
+
164
+ The run the following command to install _ protoc_ and the gRPC _ protoc_ plugin:
165
+ ``` sh
166
+ $ curl -fsSL https://goo.gl/getgrpc | bash -
167
+ ```
168
+ This will download and run the [ gRPC install script] [ ] .
134
169
135
170
<a name =" no-homebrew " ></a >
136
- ### Install protoc and the gRPC plugin without using Homebrew
171
+ ### Install _ protoc _ and the gRPC plugin without using Homebrew
137
172
138
173
First install v3 of the Protocol Buffers compiler (_ protoc_ ), by cloning
139
174
[ its Git repository] ( https://github.com/google/protobuf ) and following these
@@ -145,15 +180,15 @@ cloned.
145
180
146
181
Compile the gRPC plugins for _ protoc_ :
147
182
``` sh
148
- make plugins
183
+ make grpc_objective_c_plugin
149
184
```
150
185
151
186
Create a symbolic link to the compiled plugin binary somewhere in your ` $PATH ` :
152
187
``` sh
153
188
ln -s ` pwd` /bins/opt/grpc_objective_c_plugin /usr/local/bin/protoc-gen-objcgrpc
154
189
```
155
- (Notice that the name of the created link must begin with "protoc-gen-" for _ protoc_ to recognize it
156
- as a plugin).
190
+ (Notice that the name of the created link must begin with "` protoc-gen- ` " for _ protoc_ to recognize
191
+ it as a plugin).
157
192
158
193
If you don't want to create the symbolic link, you can alternatively copy the binary (with the
159
194
appropriate name). Or you might prefer instead to specify the plugin's path as a flag when invoking
@@ -178,5 +213,5 @@ Objective-C Protobuf runtime library.
178
213
[ Protocol Buffers ] :https://developers.google.com/protocol-buffers/
179
214
[ homebrew ] :http://brew.sh
180
215
[ gRPC install script ] :https://raw.githubusercontent.com/grpc/homebrew-grpc/master/scripts/install
181
- [ example Podfile ] :https://github.com/grpc/grpc/blob/master/src /objective-c/examples/Sample /Podfile
182
- [ sample app ] : https://github.com/grpc/grpc/tree/master/src /objective-c/examples/Sample
216
+ [ example Podfile ] :https://github.com/grpc/grpc/blob/master/examples /objective-c/helloworld /Podfile
217
+ [ example apps ] : https://github.com/grpc/grpc/tree/master/examples /objective-c
0 commit comments