Follow @ServiceStack or join the Google+ Community for updates, or StackOverflow or the Customer Forums for support.
See Swift Add ServiceStack Reference for an overview of the Swift Support in ServiceStack.
ServiceStack's Add ServiceStack Reference feature lets iOS/macOS developers easily generate an native
typed Swift API for your ServiceStack Services using the x
dotnet command-line tool.
The x dotnet tool provides a simple command-line UX to easily Add and Update Swift ServiceStack References.
Prerequisites: Install .NET Core.
$ dotnet tool install --global x
This will make the x
dotnet tool available in your $PATH
which can now be used from within a Terminal window at your Xcode project folder.
To use the latest JsonServiceClient
you'll need to add a reference to ServiceStack Swift library using your preferred package manager:
From Xcode 12 the Swift Package Manager is built into Xcode.
Go to File > Swift Packages > Add Package Dependency:
Add a reference to the ServiceStack.Swift GitHub repo:
https://github.com/ServiceStack/ServiceStack.Swift
After adding the dependency both ServiceStack.Swift and its PromiseKit dependency will be added to your project:
dependencies: [
.package(name: "ServiceStack", url: "https://github.com/ServiceStack/ServiceStack.Swift.git",
Version(6,0,0)..<Version(7,0,0)),
],
In your Podfile:
use_frameworks!
# Pods for Project
pod "ServiceStack", '~> 6.0.2'
github "ServiceStack/ServiceStack.Swift" ~> 6.0.2
Added new sync and async file upload with Request APIs for POST and PUT HTTP Requests:
protocol ServiceClient {
func postFileWithRequest<T: IReturn>(_ relativeUrl: String, request:T, fileName:String, data:Data, mimeType:String?, fieldName:String?) throws -> T.Return
func postFileWithRequestAsync<T: IReturn>(_ relativeUrl: String, request:T, fileName:String, data:Data, mimeType:String?, fieldName:String?) async throws -> T.Return
func postFileWithRequest<T: IReturn>(url:URL, request:T, fileName:String, data:Data, mimeType:String?, fieldName:String?) throws -> T.Return
func postFileWithRequestAsync<T: IReturn>(url:URL, request:T, fileName:String, data:Data, mimeType:String?, fieldName:String?) async throws -> T.Return
func postFilesWithRequest<T: IReturn & Codable>(request:T, files:[UploadFile]) throws -> T.Return
func postFilesWithRequestAsync<T: IReturn & Codable>(request:T, files:[UploadFile]) async throws -> T.Return
func postFilesWithRequest<T: IReturn>(url:URL, request:T, files:[UploadFile]) throws -> T.Return
func postFilesWithRequestAsync<T: IReturn>(url:URL, request:T, files:[UploadFile]) async throws -> T.Return
func putFileWithRequest<T: IReturn>(_ relativeUrl: String, request:T, fileName:String, data:Data, mimeType:String?, fieldName:String?) throws -> T.Return
func putFileWithRequestAsync<T: IReturn>(_ relativeUrl: String, request:T, fileName:String, data:Data, mimeType:String?, fieldName:String?) async throws -> T.Return
func putFileWithRequest<T: IReturn>(url:URL, request:T, fileName:String, data:Data, mimeType:String?, fieldName:String?) throws -> T.Return
func putFileWithRequestAsync<T: IReturn>(url:URL, request:T, fileName:String, data:Data, mimeType:String?, fieldName:String?) async throws -> T.Return
func putFilesWithRequest<T: IReturn & Codable>(request:T, files:[UploadFile]) throws -> T.Return
func putFilesWithRequestAsync<T: IReturn & Codable>(request:T, files:[UploadFile]) async throws -> T.Return
func putFilesWithRequest<T: IReturn>(url:URL, request:T, files:[UploadFile]) throws -> T.Return
func putFilesWithRequestAsync<T: IReturn>(url:URL, request:T, files:[UploadFile]) async throws -> T.Return
func sendFileWithRequest<T: IReturn>(_ req:inout URLRequest, request:T, fileName:String, data:Data, mimeType:String?, fieldName:String?) throws -> T.Return
func sendFileWithRequestAsync<T: IReturn>(_ req:inout URLRequest, request:T, fileName:String, data:Data, mimeType:String?, fieldName:String?) async throws -> T.Return
func sendFilesWithRequest<T: IReturn>(_ req:inout URLRequest, request:T, files:[UploadFile]) throws -> T.Return
func sendFilesWithRequestAsync<T: IReturn>(_ req:inout URLRequest, request:T, files:[UploadFile]) async throws -> T.Return
}
The latest v6 Release is now dependency-free, where its PromiseKit async APIs have been replaced to use Swift's native Concurrency support.
import ServiceStack
let client = JsonServiceClient(baseUrl:baseUrl)
let request = Hello()
request.name = "World"
let response = try await client.postAsync(request)
print(response.result!)
let request = Hello()
request.name = "World"
let response = try client.post(request)
print(response.result!)
The latest v5 support for ServiceStack.Swift has been rewritten to use Swift 5 and DTOs generated using Swift's new Codable
available in ServiceStack from v5.10.5+.
To use a JsonServiceStack
with DTOs generated earlier ServiceStack versions you'll need to reference the older 1.x client version instead:
dependencies: [
.package(name: "ServiceStack", url: "https://github.com/ServiceStack/ServiceStack.Swift.git",
Version(1,0,0)..<Version(2,0,0)),
],
To Add a new ServiceStack Reference, call x swift
with the Base URL to a remote ServiceStack instance:
x swift {BaseUrl}
x swift {BaseUrl} {FileName}
Where if no FileName is provided, it first uses dtos.swift
or if it exists the filename is inferred from the host name of the remote URL, e.g:
x swift https://techstacks.io
Downloads the Typed Swift DTOs for techstacks.io and saves them to dtos.swift
.
Alternatively you can have it saved to a different FileName with:
x swift https://techstacks.io TechStacks
Which instead saves the DTOs to TechStacks.dtos.swift
.
x swift
also downloads ServiceStack's Swift Client
and saves it to JsonServiceClient.swift
which together with the Server DTOs contains all the dependencies
required to consume Typed Web Services in Swift.
The easiest way to update all your Swift Server DTOs is to just call x swift
without any arguments:
x swift
This will go through and update all your *.dtos.swift
Service References.
To Update a specific ServiceStack Reference, call x swift
with the Filename:
x swift {FileName.dtos.swift}
As an example, you can Update the Server DTOs added in the previous command with:
x swift TechStacks.dtos.swift
Which also includes any Customization Options that were manually added.
Refer to Swift Add ServiceStack Reference docs for info on additional customizations available.
AutoQuery Viewer is a native iPad App that provides an automatic UI for browsing, inspecting and querying any publicly accessible ServiceStack AutoQuery Service from an iPad.
The TechStacks Native iOS App provides a fluid and responsive experience for browsing https://techstacks.io content on iPhones and iPad devices. It takes advantage of the ease-of-use and utility of ServiceStack's new support for Swift and XCode for quickly building services-rich iOS Apps. Get it now free on the AppStore!
TechStacks OSX Desktop App is built around 2 AutoQuery Services showing how much querying functionality AutoQuery Services provides for free and how easy they are to call with ServiceStack's new support for Swift and XCode.