iOS-Swift Getting Started

This quickstart guide contains individual steps which demonstrate how to use various Auth0 features in your iOS applications. Each step has a dedicated sample project which can be downloaded directly from the quickstart guide or forked on Github.

About the Sample Applications

The code snippets within this quickstart guide are based on the downloadable samples. Within each of these samples, you will find a very simple application which utilizes a few ViewControllers.

It should be noted that a simplistic application is used in the samples and snippets intentionally. The focus of this quickstart guide is on how to use Auth0 in an application in general rather than on how to approach various architectural scenarios.

Auth0 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.

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.

Adding Auth0 Credentials

The dependencies listed above require your client credentials in order for them to work. If you downloaded 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

Next Tutorial
2. Login
Use Auth0 for FREECreate free Account