Docs

iOS Objective-C: Login

View on Github

iOS Objective-C: Login

Gravatar for martin.walsh@auth0.com
By Martin Walsh

This tutorial demonstrates how to add user login to an Objective-C application using Auth0. We recommend you to Log in to follow this quickstart with examples configured for your account.

I want to explore a sample app

2 minutes

Get a sample configured with your account settings or check it out on Github.

View on Github
System requirements: CocoaPods 1.2.1 | Version 8.3.2 (8E2002) | iPhone 7 - iOS 10.3 (14E269)

The preferred way to integrate Auth0 in iOS apps is using Swift. Read about it in the iOS Swift Quickstart.

New to Auth? Learn How Auth0 works, how it integrates with Native Applications and which protocol it uses.

Configure Auth0

Get Your Application Keys

When you signed up for Auth0, a new application was created for you, or you could have created a new one.

You will need some details about that application to communicate with Auth0. You can get these details from the Application Settings section in the Auth0 dashboard.

You need the following information:

  • Domain
  • Client ID

If you download the sample from the top of this page these details are filled out for you.

If you have more than one application in your account, the sample comes with the values for your Default App.

App Dashboard

Add your credentials in Auth0.plist. If the file does not exist in your project yet, create one with the information below (Apple documentation on Property List Files):

<!-- Auth0.plist -->

<?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_DOMAIN</string>
</dict>
</plist>

Configure Callback URLs

A callback URL is a URL in your application where Auth0 redirects the user after they have authenticated.

The callback URL for your app must be whitelisted in the Allowed Callback URLs field in your Application Settings. If this field is not set, users will be unable to log in to the application and will get an error.

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

<!-- Info.plist -->

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

If your Info.plist file is not in the format shown above, you can right-click Info.plist in Xcode and select Open As > Source Code.

Go to your Dashboard Settings and make sure that the Allowed Callback URLs field contains the following callback URL:

{PRODUCT_BUNDLE_IDENTIFIER}://YOUR_DOMAIN/ios/{PRODUCT_BUNDLE_IDENTIFIER}/callback

Start the Authentication

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 login dialog directly in your application using the Lock widget. 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.

Universal Login

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

Carthage

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

github "auth0/Auth0.swift" ~> 1.18

Then, run carthage bootstrap.

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

Cocoapods

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

use_frameworks!
pod 'Auth0', '~> 1.18'

Then, run pod install.

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

Set Up the Objective-C & Swift Hybrid

Configure project settings

  1. Download the HybridAuth.swift file and add it to your project. The following message appears: "Would you like to configure an Objective-C bridging header?". Click Yes.
  2. Click on your Xcode Project file.
  3. Click on Build Settings.
  4. Search for Defines Module.
  5. Change the value to Yes.
  6. Search for Product Module Name.
  7. Make a note of the name. This tutorial uses the name Auth0Sample.

Import

To import the Swift code and Auth0 library, add the following headers:

#import "Auth0Sample-Swift.h"
@import Auth0;

Add the Callback

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

Import the Swift wrapper and Auth0 library:

#import "Auth0Sample-Swift.h"
@import Auth0;

Then, add the following UIApplicationDelegate method:

// AppDelegate.m

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
    return [HybridAuth resume:url options:options];
}

Implement Login

Import the Swift wrapper and Auth0 library:

#import "Auth0Sample-Swift.h"
@import Auth0;

Then, present the hosted login screen:

// HomeViewController.m

HybridAuth *auth = [[HybridAuth alloc] init];
[auth showLoginWithScope:@"openid" connection:nil callback:^(NSError * _Nullable error, A0Credentials * _Nullable credentials) {
    dispatch_async(dispatch_get_main_queue(), ^{
        if (error) {
            NSLog(@"Error: %@", error);
        } else if (credentials) {
          // Do something with credentials, such as save them.
          // Auth0 will dismiss itself automatically by default.
        }
    });
}];

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

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

Configure Logout URLs

A logout URL is a URL in your application that Auth0 can return to after the user has been logged out of the authorization server. This is specified in the returnTo query parameter.

The logout URL for your app must be whitelisted in the Allowed Logout URLs field in your Application Settings. If this field is not set, users will be unable to log out from the application and will get an error.

Implement logout

To clear the session on the server side you need to invoke the clearSession method. Add the following snippet:

// HybridAuth.swift
@objc
func logOutUser(callback: @escaping(Bool) -> Void){
    Auth0
        .webAuth()
        .clearSession(federated: true){
            callback($0)
        }
}

Go to your Dashboard Settings and make sure that the Allowed Logout URL field contains the following logout callback URL:

{PRODUCT_BUNDLE_IDENTIFIER}://YOUR_DOMAIN/ios/{PRODUCT_BUNDLE_IDENTIFIER}/callback

After the call, the callback will receive a BOOL with the logout status.

Use Auth0 for FREE