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 Auth0 hosted login page is the easiest way to set up authentication in your application. We recommend using the Auth0 hosted login page 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.

Hosted 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

Carthage

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.

Cocoapods

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

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

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

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

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 e.g.: 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.

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