gRPC for Swift (iOS/macOS): Libraries, Setup, and Code 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 modern high-performance Remote Procedure Call (RPC) framework developed by Google. It uses Protocol Buffers (protobuf) for data serialization and HTTP/2 for transport. In the Swift ecosystem (iOS/macOS), gRPC is implemented via the gRPC-Swift library, built on top of the asynchronous framework SwiftNIO. This allows creating both client and server applications in Swift with support for streaming data, timeouts, and authentication.



Core Libraries for gRPC in Swift

  • gRPC-Swift — the official library from Apple and Google. Provides an API for creating gRPC servers and clients. Supports unary, server-streaming, client-streaming, and bidirectional streaming.
  • SwiftNIO — a low-level asynchronous networking framework on which gRPC-Swift is based.
  • protoc-gen-swiftgrpc — a plugin for the protoc compiler that generates Swift code from .proto files.
  • SwiftProtobuf — a library for working with protobuf messages in Swift (serialization/deserialization).


Installation via Swift Package Manager

Add dependencies to your project's Package.swift:

// swift-tools-version:5.7import PackageDescription

let package = Package( name: "gRPCExample", platforms: [.macOS(.v12), .iOS(.v15)], 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.21.0") ], targets: [ .executableTarget( name: "gRPCExample", dependencies: [ .product(name: "GRPC", package: "grpc-swift"), .product(name: "SwiftProtobuf", package: "swift-protobuf") ] ) ])


Creating a .proto File

Define the service and messages. Example helloworld.proto file:

syntax = "proto3";

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

message HelloRequest { string name = 1;}

message HelloReply { string message = 1;}


Generating Swift Code

Install protoc and plugins:

brew install protobufbrew install swift-protobufbrew install grpc-swift

Generate the code:

protoc helloworld.proto \    --proto_path=. \    --swift_out=. \    --grpc-swift_out=.

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



Server Example in Swift (macOS)

import GRPCimport NIOimport HelloWorld // generated module

class GreeterProvider: Greeter_GreeterProvider { func sayHello(request: Greeter_HelloRequest, context: StatusOnlyCallContext) -> EventLoopFuture<Greeter_HelloReply> { var reply = Greeter_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 Example in Swift (iOS/macOS)

import GRPCimport NIOimport HelloWorld

let group = PlatformSupport.makeEventLoopGroup(loopCount: 1)defer { try! group.syncShutdownGracefully() }

let channel = try GRPCChannelPool.with( target: .host("localhost", port: 50051), transportSecurity: .plaintext, eventLoopGroup: group)defe

Recommendations