Swift gRPC and Protobuf integration
Recently I was trying to add gRPC and SwiftProtobuf to my iOS app and I had a lot of problems doing that and was bashing my head against the wall for a whole weekend plus a Monday as well :P
So first of all if you are not familiar with gRPC and Protobuf here is the official website http://grpc.io/ .
If you have tried to search on google for something like how to create Swift gRPC app
or anything that has gRPC or Swift gRPC
you would most probably end up on the gRPC github which to be fair is a good resource but really not clear for some one that trying to integrate this awesome framework to their project for the first time.
What I am going to do now is to basically give you couple of steps that you need to do in order to get your environment set (i.e having a happy Xcode).
- clone the swift gRPC repo from https://github.com/grpc/grpc-swift
cd
togrpc-swift
and do amake
- then inside
grpc-swift
cd third_party
- in
third_party
folder you haveswift-protobuf
cd
to that - then do a
make
- so now you will have SwiftProtobuf.xcodeproj in
swift-protobuf
folder and SwiftGRPC.xcodeproj in the root directorygrpc-swift
- now it is time to add these guys to your project.
- if you are building a mac app it is a bit more straight forward, however I will describe the iOS app steps
- create a new iOS project
- drag and drop both of those .xcodeproj files into your project underneath your root project.
11. Then open up project settings and head to General tab.
12 . In Embedded bindaries click on plus and add BoringSSL, CgRPC, gRPC and Czlib frameworks.
13. If you hit build you will get at least one compile error which is saying something like can’t find CgRPC.
14. head back to project settings and select Build setting tabs and search for search paths.
15. add `<where you clone the grcp-swift git>/grpc-swift/Sources/CgRPC/include` to your System Header Search Paths
and System Framework Search Paths
16. that will fix the CgRPC
problem. However if you build again it will failed and most likely it wouldn’t even gives you the error, but if you are lucky you will see something weird about zlib-example. All you need to do is to select SwiftGRPC.xcodeproj
from your Xcode project naviagator and in the targets find that zlib-example and delete. The issue with that target is, that target has been built for macOS so it wouldn’t work with an iOS project, however if you are adding the gRPC to a mac app you wouldn’t need to do this step.
That was it. I hope this would save couple of hours of searching and head banging :)
Note: At the time of writing this Article I was using Xcode 9 with Swift 4, SwiftGRPC version 4.0.0 and SwiftProtobuf version 0.9.905.