iOS-Swift Login

This multi-step quickstart guide will walk you through managing authentication in your iOS apps with Auth0.

Sample Projects

Each tutorial in the series includes a link to its corresponding sample project, which demonstrates how to achieve the tutorial's goal. You can find all the samples here.

Dependencies

Each tutorial will require you to use either Lock or the Auth0.swift toolkit, or both.

A brief description:

  • Lock is a widget that is easy to present in your app. It contains default templates (that can be customized) for login with email/password, signup, social providers integration, and password recovery.
  • Auth0.swift is a toolkit that lets you communicate efficiently with many of the basic Auth0 API functions.

Universal Links

Because universal links establish a verified relationship between domains and applications, both your Auth0 Client settings and your iOS application need to be in sync. You can view instructions on setting up universal links here.

Carthage

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

github "auth0/Lock.iOS-OSX" ~> 2.0
github "auth0/Auth0.swift" ~> 1.2

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.2'

Then, run pod install.

For further reference on Cocoapods, check their official documentation.

Application Keys

When you signed up for Auth0, you were invited to create a new client.

There are some details about this client that your application needs to know about to properly communicate with Auth0. These include your Client ID, Domain, and Client Secret. You can retrieve these values from the settings area for your client in the Auth0 dashboard.

Please note that if you download the samples available for this tutorial, these keys will be pre-populated for you. If you have created more than one client in your account, the sample will come with the values for your Default App.

App Dashboard

Adding Auth0 Credentials

The dependencies listed above requires that you set your credentials in two different .plist files in order for them to work. If you downloaded the seed project, or any sample project from here, these credentials are automatically set. Either way, you have to make sure they are there otherwise your app might crash.

Add your credentials in Auth0.plist. You have to create that file if it doesn't already exist in your project:

<?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>

Configure Callback URLs

Callback URLs are the URLs that Auth0 invokes after the authentication process. Auth0 routes your application back to this URL and appends additional parameters to it, including a token. Since callback URLs can be manipulated, you will need to add your application's URL to your client's Allowed Callback URLs for security. This will enable Auth0 to recognize these URLs as valid. If omitted, authentication will not be successful.

In your application's Info.plist file, register your iOS Bundle Identifer as a custom scheme:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleTypeRole</key>
        <string>None</string>
        <key>CFBundleURLName</key>
        <string>auth0</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
        </array>
    </dict>
</array>

Finally, go to your Client's Dashboard and make sure that Allowed Callback URLs contains the following:

{YOUR_BUNDLE_IDENTIFIER}://{YOUR_AUTH0_DOMAIN}/ios/{YOUR_BUNDLE_IDENTIFIER}/callback

Sample Project

Download this sample project configured with your Auth0 API Keys.

System Requirements
  • CocoaPods 1.1.1
  • Version 8.2 (8C38)
  • iPhone 6 - iOS 10.2 (14C89)
Show requirements

Implement the Login

First, import the Lock module in the file where you want to present the login dialog:

import Lock

Then, configure and present the login screen, like this:

Lock
    .classic()
    .withOptions {
        $0.oidcConformant = true
    }
    .onAuth { credentials in
        // Do something with credentials e.g.: save them.
        // Lock will not save these objects for you.
        // Lock will dismiss itself automatically by default.
    }
    .present(from: self)
Lock UI

There are multiple ways of implementing the login dialog. What you see above is the default widget; however, if you want, you can use your own custom login.

As you can see, upon successful authentication, the onAuth callback will yield the user's credentials.

For further reference on the credentials object, please see: Credentials

We will cover the storing of the users credentials in a later chapter. By default Lock will not store this for you.

This sets you up for handling Database connections.

Log In with Enterprise & Social Connections

In order to use browser based Auth mechanism through social and enterprise connections, all you have to do is enable them in your account's connections dashboard. Every connection you switch on there, will appear in the Login screen of your app.

Lock will need to handle the callback of this authentication, Lock can take responsibility for this by adding the following to your AppDelegate:

First, import the Lock module:

import Lock

Then, add the following UIApplicationDelegate method:

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
  return Lock.resumeAuth(url, options: options)
}

Next Tutorial
2. Custom Login
Use Auth0 for FREECreate free Account