iOS SDK
Integrate your iOS application with Authgear iOS SDK

Setup Application in Authgear

Signup for an account in https://portal.authgearapps.com/ and create a project. Or you can use your self-deployed Authgear.
After that, we will need to create an application in Authgear.
Portal
authgear.yaml (self-deployed)
Create an application
  1. 1.
    Go to "Applications".
  2. 2.
    Click "Add Application" in the top right corner
  3. 3.
    Input name of your application, this is for reference only
  4. 4.
    Defining a custom scheme that the users will be redirected back to your app after they have authenticated with Authgear. Add the URI to "Redirect URIs". (e.g. com.myapp://host/path).
  5. 5.
    Click "Save" and keep the client id. You can also obtain the client id from the list later.
If you want to validate JWT access token in your server, select Issue JWT as access token. If you will forward incoming requests to Authgear Resolver Endpoint for authentication, leave this unchecked. See comparisons in Backend Integration.
1
oauth:
2
clients:
3
- name: your_app_name
4
client_id: a_random_generated_string
5
redirect_uris:
6
- "com.myapp://host/path"
7
grant_types:
8
- authorization_code
9
- refresh_token
10
response_types:
11
- code
12
- none
Copied!

Install the SDK

CocoaPods

1
pod 'Authgear', :git => 'https://github.com/authgear/authgear-sdk-ios.git'
Copied!

Swift Package Manager

1
dependencies: [
2
.package(url: "https://github.com/authgear/authgear-sdk-ios.git")
3
]
Copied!

Register URL Scheme for Redirect URI

In your application's Info.plist , register your custom URL scheme, (e.g. com.myapp).
1
<?xml version="1.0" encoding="UTF-8"?>
2
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3
<plist version="1.0">
4
<dict>
5
<!-- Other entries -->
6
<key>CFBundleURLTypes</key>
7
<array>
8
<dict>
9
<key>CFBundleTypeRole</key>
10
<string>Editor</string>
11
<key>CFBundleURLSchemes</key>
12
<array>
13
<string>{YOUR_CUSTOM_URL_SCHEME}</string>
14
</array>
15
</dict>
16
</array>
17
</dict>
18
</plist>
Copied!

Configuration Authgear SDK

SDK must be properly configured before use.
1
// your_app_endpoint should looks like this https://<yourapp>.authgearapps.com
2
let authgear = Authgear(clientId: "{your_clien_id}", endpoint: "{your_app_endpoint}")
3
authgear.configure() { result in
4
switch result {
5
case .success():
6
// configured successfully
7
case let .failure(error):
8
// failed to configured
9
}
10
}
Copied!

Trigger authenticate

When the user clicks login/signup on your app, you can use the following code to start authorization.
1
// your_redirect_uri is redirect uri registered on the applications page
2
// e.g. com.myapp://host/path
3
authgear.authorize(redirectURI: "{your_redirect_uri}", handler: { result in
4
switch result {
5
case let .success(authResult):
6
let userInfo = authResult.userInfo
7
// login successfully
8
case let .failure(error):
9
// failed to login
10
}
11
})
Copied!
Your user is now logged in!

Using the Access Token in HTTP Requests

Call refreshAccessTokenIfNeeded every time before using the access token, the function will check and make the network call only if the access token has expired. Include the access token into the Authorization header of your application request.
1
authgear.refreshAccessTokenIfNeeded() { result in
2
switch result {
3
case .success():
4
// access token is ready to use
5
// accessToken can be empty
6
// it will be empty if user is not logged in or session is invalid
7
8
// include Authorization header in your application request
9
if let accessToken = authgear.accessToken {
10
// example only, you can use your own networking library
11
var urlRequest = URLRequest(url: "YOUR_SERVER_URL")
12
urlRequest.setValue(
13
"Bearer \(accessToken)", forHTTPHeaderField: "authorization")
14
// ... continue making your request
15
}
16
case let .failure(error):
17
// failed to refresh access token
18
// the refresh token maybe expired or revoked
19
}
20
}
Copied!

Logout

To log out the user from the current app session, you need to invoke thelogoutfunction.
1
authgear.logout { result in
2
switch result {
3
case .success():
4
// logout successfully
5
case let .failure(error):
6
// failed to login
7
}
8
}
Copied!

Next steps

To protect your application server from unauthorized access. You will need to integrate your backend with Authgear.
Last modified 5mo ago