iOS Objective-C 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.

Hybrid Objective-C / Swift

Project settings

  1. Download the HybridAuth.swift file and add this to your project.
  2. A popup window will appear and ask "Would You like to configure an Objective-C bridging Header". Choose Yes
  3. Click on your Xcode Project file
  4. Click on Build Settings
  5. Find the Search bar and search for Defines Module.
  6. Change the value to Yes.
  7. Search Product Module Name.
  8. Make a note of the name, we will be using Auth0Sample.

Import

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

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

Adding the callback

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

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];
}

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

Implement the Login

Import the Swift wrapper and Auth0 library:

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

Then present the hosted login screen, like this:

// 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 e.g.: save them.
          // Auth0 will dismiss itself automatically by default.
        }
    });
}];

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

For further reference on the credentials object, please see Credentials. We will cover the storage of the user's credentials in a later chapter. By default Auth0 will not store this for you.

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