gRPC for Swift (iOS/macOS): Libraries, Setup, and Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

gRPC for Swift (iOS/macOS): Libraries and Examples

gRPC is a high-performance remote procedure call (RPC) framework developed by Google. It uses Protocol Buffers (protobuf) for data serialization and HTTP/2 for transport. For Apple platforms (iOS, macOS), there is an official Swift gRPC ecosystem that allows you to create both client and server applications with full support for modern Swift features, including async/await.



Core Libraries and Tools

The following components are used to work with gRPC in Swift:

  • gRPC-Swift — the official package from Google. It includes the runtime, code generator, and support for client-server interaction.
  • Swift Protobuf — a library for working with protobuf messages. Required for generating Swift structures from .proto files.
  • protoc — the Protocol Buffers compiler. To generate Swift code, the protoc-gen-grpc-swift and protoc-gen-swift plugins are required.
  • Swift Package Manager (SPM) — the primary way to integrate dependencies.


Installation and Setup

To get started, add the dependencies to Package.swift:

// swift-tools-version:5.9import PackageDescription

let package = Package( name: "GRPCExample", dependencies: [ .package(url: "https://github.com/grpc/grpc-swift.git", from: "1.21.0"), .package(url: "https://github.com/apple/swift-protobuf.git", from: "1.26.0") ], targets: [ .executableTarget( name: "GRPCExample", dependencies: [ .product(name: "GRPC", package: "grpc-swift"), .product(name: "SwiftProtobuf", package: "swift-protobuf") ] ) ])


You also need to install protoc and the plugins via Homebrew:

brew install protobufbrew install grpc-swiftbrew install swift-protobuf


Example .proto File

Let's create a simple greeting service. File hello.proto:

syntax = "proto3";

service Greeter { rpc SayHello (HelloRequest) returns (HelloReply);}

message HelloRequest { string name = 1;}

message HelloReply { string message = 1;}


Generate Swift code with the command:

protoc --swift_out=. --grpc-swift_out=. hello.proto


This will create the files hello.pb.swift and hello.grpc.swift.



Server-Side in Swift

Let's create a gRPC server that will handle SayHello requests:

import GRPCimport NIOimport NIOPosix

class GreeterProvider: GreeterProvider { func sayHello(request: HelloRequest, context: StatusOnlyCallContext) -> EventLoopFuture<HelloReply> { var reply = HelloReply() reply.message = "Hello, \\(request.name)!" return context.eventLoop.makeSucceededFuture(reply) }}

let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)defer { try! group.syncShutdownGracefully() }

let server = try Server.insecure(group: group) .withServiceProviders([GreeterProvider()]) .bind(host: "localhost", port: 50051) .wait()

print("Server started on localhost:50051")try server.onClose.wait()


Client-Side in Swift (with async/await)

For iOS/macOS clients, it is convenient to implement using async/await. Make sure you are using Swift 5.5+ and a minimum deployment target of iOS 13.0 / macOS 10.15.

import GRPCimport NIOimport NIOPosiximport SwiftProtobuf

func greet(name: String) async throws -> String { let group = PlatformSupport.makeEventLoopGroup(loopCount: 1) defer { try? group.syncShutdownGracefully() }\

Recommendations