iOS Swift Login

Sample Project

Download a sample project specific to this tutorial configured with your Auth0 API Keys.

System Requirements
  • CocoaPods 1.2.1
  • Version 8.3.2 (8E2002)
  • iPhone 7 - iOS 10.3 (14E269)
Show requirements

The first step in adding authentication to your iOS application is to provide a way for your users to log in. The fastest, most secure, and most feature-rich way to do this with Auth0 is to use the login page.

Hosted Login Page

Install

Carthage

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

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 'Auth0', '~> 1.0'

Then, run pod install.

For further reference on Cocoapods, check their official documentation.

Adding the callback

Auth0 will need to handle the callback of this authentication, add the following to your AppDelegate:

First, import the Auth0 module:

import Auth0

Then, add the following UIApplicationDelegate method:

// AppDelegate.swift

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

Please ensure you have configured your callback URL, as demonstrated in Configure Callback.

Implement the Login

First, import the Auth0 module in the file where you want to present the hosted login page:

import Auth0

Then present the hosted login screen, like this:

// HomeViewController.swift

Auth0
    .webAuth()
    .scope("openid profile")
    .audience("https://YOUR_AUTH0_DOMAIN/userinfo")
    .start {
        switch $0 {
        case .failure(let error):
            // Handle the error
            print("Error: \(error)")
        case .success(let credentials):
            // Do something with credentials e.g.: save them.
            // Auth0 will automatically dismiss the hosted login page
            print("Credentials: \(credentials)")
        }
}

This snippet sets the audience to ensure an OIDC compliant response. This can also be achieved by enabling the OIDC Conformant switch in your Auth0 dashboard, under Client > Settings > Show Advanced Settings > OAuth. For more information, refer to How to use the new flows.

Upon successful authentication the user's credentials will be returned.

For more information on the credentials object, refer to Credentials. We will cover the storage of the user's credentials in a later chapter. By default Auth0 will not store this for you.

User Profile

The login snippet adds the profile scope to enable retrieving the User Profile.

Embedded Login

Auth0's hosted login page provides the fastest, most secure, and most feature-rich way to implement authentication in your app. If required, the Lock widget can also be embedded directly into your application, but certain features such as single sign-on won't be accessible. It is highly recommended that you use this login (as covered in this tutorial), but if you wish to embed the Lock widget directly in your application, you can follow the Embedded Login sample.

Please see Browser-Based vs. Native Login Flows on Mobile Devices for information on choosing between the two types of login flows.

Previous Tutorial
1. Getting Started
Next Tutorial
3. User Sessions
Use Auth0 for FREECreate free Account