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

Universal login is the easiest way to set up authentication in your application. We recommend using it for the best experience, best security and the fullest array of features.

You can also embed the Lock widget directly in your application. If you use this method, some features, such as single sign-on, will not be accessible. To learn how to embed the Lock widget in your application, follow the Embedded Login sample.

login page

Read the Browser-Based vs. Native Login Flows on Mobile Devices article to learn how to choose between the two types of login flows.

Install Dependencies


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

github "auth0/Auth0.swift" ~> 1.0

Then, run carthage bootstrap.

For more information on how to use Carthage, read their official documentation.


If you are using Cocoapods, add the following to your Podfile:

pod 'Auth0', '~> 1.0'

Then, run pod install.

For more information on how to use Cocoapods, read the Cocoapods documentation.

Add the Callback

For Auth0 to handle the authentication callback, update your AppDelegate file.

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)

To configure callback, you must configure your callback URL first. Read about the Callback URL in the Configure Callback step.

Implement Login

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

import Auth0

Then, present the login screen:

// HomeViewController.swift

    .scope("openid profile")
    .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 login page
            print("Credentials: \(credentials)")

This adds the profile scope to enable retrieving the User Profile.

You need to make sure you get a response compliant with the OpenID Connect protocol. You can choose between two options:

  • Set the audience
  • Turn on the OIDC conformant switch in your Auth0 dashboard

This example sets the audience parameter to get an OIDC-compliant response.

To turn on the OIDC conformant switch, in your Client Settings, click on Show Advanced Settings > OAuth. For more information, read How to use the new flows.

After the user authenticates, their information is returned in a credentials object.

To learn more about the credentials object, read the Credentials article.

Previous Tutorial
1. Getting Started
Next Tutorial
3. User Sessions
Was this article helpful?
Use Auth0 for FREECreate free Account