Swift SDK for iOS
On this page
The iOS SDK adds OAuth 2.0 and OpenID Connect login to an iOS app, with FusionAuth as the authorization serverThe server that authenticates users and issues tokens to applications in OAuth 2.0. FusionAuth is an authorization server.. The SDK also provides a Token Manager that stores, refreshes, and retrieves tokens. The SDK handles the dependencies for you, so you get a standardized starting point for a custom mobile app.
The SDK covers the following functionality:
- OAuth 2.0 Authorization Code Grant
- OAuth 2.0 Refresh Token Grant
- OpenID Connect UserInfo
- OpenID Connect End Session
The SDK uses AppAuth-iOS for the Authorization Code Grant flow and the OpenID Connect functionality. Swift is the SDK language, and the SDK works with Objective-C.
Get Started#
When you are new to iOS development, start with the QuickStart. When you already know iOS development, skip to Configure the SDK.
QuickStart#
For a full tutorial on using FusionAuth with iOS, see the FusionAuth iOS QuickStart.
Configure the SDK#
-
Add the SDK repository as a dependency of your app.
-
Create a
FusionAuth.plistfile in the main bundle. The SDK reads the configuration from this file by default, and the file contains the following keys:<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>additionalScopes</key> <array> <string>profile</string> <string>email</string> </array> <key>fusionAuthUrl</key> <string>http://localhost:9011</string> <key>clientId</key> <string>e9fdb985-9173-4e01-9d73-ac2d60d1dc8e</string> </dict> </plist> -
Initialize the
AuthorizationManagerin theinitfunction of your app:@main struct QuickstartApp: App { init() { AuthorizationManager.instance.initialize() } } -
Handle the callback from the browser:
@main struct QuickstartApp: App { init() { AuthorizationManager.instance.initialize() } var body: some Scene { WindowGroup { ContentView() .onOpenURL { url in OAuthAuthorization.resume(with: url) } } } }
Set the Configuration in Code#
You can pass the configuration to the initialize function instead of using the FusionAuth.plist file:
@main
struct QuickstartApp: App {
init() {
AuthorizationManager.instance.initialize(configuration: AuthorizationConfiguration(
clientId: "e9fdb985-9173-4e01-9d73-ac2d60d1dc8e",
fusionAuthUrl: "http://localhost:9011",
additionalScopes: ["email", "profile"]))
}
}
Store Tokens#
The SDK stores tokens in MemoryStorage by default. To use KeyChainStorage or UserDefaultsStorage instead, add the storage key to the FusionAuth.plist file with the value keychain or userdefaults.
Use the SDK#
The following steps cover the login, token, and logout flow.
-
Call the
oauth()function on theAuthorizationManagerto get theOAuthAuthorizationService, and then callauthorizeto start the OAuth 2.0 Authorization Code Grant:do { try await AuthorizationManager .oauth() .authorize(options: OAuthAuthorizeOptions()) } catch let error as NSError { print("Error: \(error.localizedDescription)") }The
authorizemethod opens the Safari browser and redirects to the FusionAuth login page. After a successful login, FusionAuth redirects the user back to the app with the authorization code. The SDK validates thestatewhen you provided one, exchanges the authorization code for an access token and a refresh token, and stores the result in theTokenManager. -
Call
userInfo()to retrieve the User Info after the user is authorized:do { self.userInfo = try await AuthorizationManager .oauth() .userInfo() } catch let error as NSError { print("JSON decode failed: \(error.localizedDescription)") } -
Call
freshAccessTokento get a valid access token for your API calls:let accessToken = AuthorizationManager.oauth().freshAccessToken()The
TokenManagerreturns a fresh access token. When the access token has expired, theTokenManagerrefreshes the access token automatically. -
Call
logoutto sign out the user and remove the tokens from theTokenManager:do { try await AuthorizationManager .oauth() .logout(options: OAuthLogoutOptions()) } catch let error as NSError { print("Error: \(error.localizedDescription)") }The SDK clears the auth state after the user signs out.
Example App#
For a functional example of an iOS client that uses the SDK, see the FusionAuth iOS SDK Example.
Documentation#
For the complete documentation of the SDK, see the latest full library documentation.
Source Code#
For the source code, see the Swift SDK repository.