Lock v2 for iOS

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 or later
  • Xcode 8
  • Swift 3.0

Install

Carthage

If you are using Carthage, add the following lines to your Cartfile:

github "auth0/Lock.swift" ~> 2.0
github "auth0/Auth0.swift" ~> 1.0

Then run carthage bootstrap.

For more information about Carthage usage, check their official documentation.

Cocoapods

If you are using Cocoapods, add these lines to your Podfile:

use_frameworks!
pod 'Lock', '~> 2.0'
pod 'Auth0', '~> 1.0'

Then, run pod install.

For further reference on Cocoapods, check their official documentation.

Setup

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: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
  return Lock.resumeAuth(url, options: options)
}

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 Client'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>YOUR_CLIENT_ID</string>
  <key>Domain</key>
  <string>YOUR_AUTH0_DOMAIN</string>
</dict>
</plist>

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
}

For more information, please see our Introduction to OIDC Conformant Authentication and the OIDC adoption guide.

To show Lock, add the following snippet in your UIViewController.

Lock
    .classic()
    // withConnections, withOptions, withStyle, etc
    .withOptions {
      $0.oidcConformant = true
      $0.scope = "openid profile"
    }
    .onAuth { credentials in
      // Let's save our credentials.accessToken value
    }
    .present(from: self)

Use Auth0.Swift Library to access user profile

To access user profile information, you will need to use the Auth0.Swift library:

guard let accessToken = credentials.accessToken else { return }
Auth0
    .authentication()
    .userInfo(withAccessToken: accessToken)
    .start { result in
        switch result {
        case .success(let profile):
            // You've got a UserProfile object
        case .failure(let error):
            // You've got an error
        }
}

Check out the Auth0.Swift Library Documentation for more information about its uses.

Specify Connections

Lock will automatically load the connections configured for your client. 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)
}

Adding multiple social connections:

.withConnections {
    connections.social(name: "facebook", style: .Facebook)
    connections.social(name: "google-oauth2", style: .Google)
}

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)
}

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]
  }

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
}

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"
}

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>

Next Steps