Lock.swift
This reference guide will show you how to implement the Lock user interface, and give you the details on configuring and customizing Lock in order to use it as the UI for your authentication needs. However, if you'd like to learn how to do more with Auth0 and Swift, such as how to save, call and refresh Access Tokens, get user profile info, and more, check out the Auth0.swift SDK. Or, take a look at the Swift Quickstart to walk through complete examples and see options, both for using Lock as the interface, and for using a custom interface.
Check out the Lock.swift repository on GitHub.
Requirements
iOS 9+
Xcode 11.4+ / 12.x
Swift 4.x / 5.x
Install
Cocoapods
If you are using Cocoapods, add this line to your Podfile
:
pod 'Lock', '~> 2.0'
Then run pod install
.
For more information on Cocoapods, check their official documentation.
Carthage
If you are using Carthage, add the following line to your Cartfile
:
github "auth0/Lock.swift" ~> 2.0
Then run carthage bootstrap
.
For more information about Carthage usage, check their official documentation.
SPM
If you are using the Swift Package Manager, open the following menu item in Xcode:
File > Swift Packages > Add Package Dependency...
In the Choose Package Repository prompt add this url:
https://github.com/auth0/Lock.swift.git
Then press Next and complete the remaining steps.
Set up
Integrate with your Application
Lock needs to be notified when the application is asked to open a URL. You can do this in the AppDelegate
file.
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
return Lock.resumeAuth(url, options: options)
}
Was this helpful?
Import Lock
Import Lock wherever you'll need it
import lock
Auth0 Credentials
In order to use Lock you need to provide your Auth0 Client Id and Domain, which can be found in your Auth0 Dashboard, under your Application's settings.
In your application bundle you can add a plist
file named Auth0.plist
that will include your credentials with the following format.
<?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>ClientId</key>
<string>{yourClientId}</string>
<key>Domain</key>
<string>{yourDomain}</string>
</dict>
</plist>
Was this helpful?
Implementation of Lock Classic
Lock Classic handles authentication using Database, Social, and Enterprise connections.
OIDC Conformant Mode
It is strongly encouraged that this SDK be used in OIDC Conformant mode. When this mode is enabled, it will force the SDK to use Auth0's current authentication pipeline and will prevent it from reaching legacy endpoints. By default this is false
.withOptions {
$0.oidcConformant = true
}
Was this helpful?
To show Lock, add the following snippet in your UIViewController.
Lock
.classic()
// withConnections, withOptions, withStyle, and so on
.withOptions {
$0.oidcConformant = true
$0.scope = "openid profile"
}
.onAuth { credentials in
// Let's save our credentials.accessToken value
}
.present(from: self)
Was this helpful?
Use Auth0.Swift Library to access user profile
To access user profile information, you will need to use the Auth0.Swift
library:
Auth0
.authentication()
.userInfo(withAccessToken: accessToken)
.start { result in
switch result {
case .success(let profile):
print("User Profile: \(profile)")
case .failure(let error):
print("Failed with \(error)")
}
}
Was this helpful?
Check out the Auth0.Swift Library Documentation for more information about its uses.
Specify Connections
Lock will automatically load the connections configured for your application. If you wish to override the default behavior, you can manually specify which connections it should display to users as authentication options. This can be done by calling the method and supplying a closure that can specify the connection(s).
Adding a database connection:
.withConnections {
connections.database(name: "Username-Password-Authentication", requiresUsername: true)
}
Was this helpful?
Adding multiple social connections:
.withConnections {
connections.social(name: "facebook", style: .Facebook)
connections.social(name: "google-oauth2", style: .Google)
}
Was this helpful?
Styling and Customization
Lock provides many styling options to help you apply your own brand identity to Lock using withStyle
. For example, changing the primary color and header text of your Lock widget:
Customize your title, logo, and primary color
.withStyle {
$0.title = "Company LLC"
$0.logo = LazyImage(named: "company_logo")
$0.primaryColor = UIColor(red: 0.6784, green: 0.5412, blue: 0.7333, alpha: 1.0)
}
Was this helpful?
You can see the complete set of styling options to alter the appearance of Lock for your app in the Customization Guide.
Configuration Options
There are numerous options to configure Lock's behavior. Below is an example of Lock configured to allow it to be closable, to limit it to only usernames (and not emails), and to only show the Login and Reset Password screens.
Lock
.classic()
.withOptions {
$0.closable = true
$0.usernameStyle = [.Username]
$0.allow = [.Login, .ResetPassword]
}
Was this helpful?
You can see the complete set of behavior configuration options to alter the way Lock works for your app in the Configuration Guide.
Password Manager Support
By default, password manager support using 1Password is enabled for database connections. 1Password support will still require the user to have the 1Password app installed for the option to be visible in the login and signup screens. You can disable 1Password support using the enabled property of the passwordManager.
.withOptions {
$0.passwordManager.enabled = false
}
Was this helpful?
By default the appIdentifier
will be set to the app's bundle identifier and the displayName
will be set to the app's display name. You can customize these as follows:
.withOptions {
$0.passwordManager.appIdentifier = "www.myapp.com"
$0.passwordManager.displayName = "My App"
}
Was this helpful?
You will need to add the following to your app's info.plist
:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>org-appextension-feature-password-management</string>
</array>
Was this helpful?