iOS - Objective C

Sample Project

Download this sample project configured with your Auth0 API Keys.

System Requirements
  • CocoaPods 0.39.0
  • XCode 7.2.1
  • Simulator - iOS 9.2 - iPhone 6
Show requirements

Initial Setup

Go to the Application Settings section of your app in the Auth0 dashboard and make sure that Allowed Callback URLs contains the following value:

a0YOUR_CLIENT_ID://\*.auth0.com/authorize

Add the Auth0 Dependencies

Add the following to the Podfile and run pod install:

pod 'Lock', '~> 1.24'

NOTE: If you need help installing CocoaPods, see: What is CocoaPods.

Configure Auth0 Lock for iOS

  1. Add the following entries to your app's Info.plist:
Key Value
Auth0ClientId YOUR_CLIENT_ID
Auth0Domain YOUR_AUTH0_DOMAIN
  1. Register a new URL Type in your app's Targets info section with the following scheme: a0YOUR_CLIENT_ID

    Lock.png

  2. Create and configure an instance of A0Lock with your Auth0 credentials from Info.plist. This sample uses a custom object called MyApplication:

@class A0Lock;
@interface MyApplication : NSObject
@property (readonly, nonatomic) A0Lock *lock;
+ (MyApplication *)sharedInstance;
@end
#import "MyApplication.h"
#import <Lock/Lock.h>

@implementation MyApplication
+ (MyApplication*)sharedInstance {
    static MyApplication *sharedApplication = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedApplication = [[self alloc] init];
    });
    return sharedApplication;
}

- (id)init {
    self = [super init];
    if (self) {
        _lock = [A0Lock newLock];
    }
    return self;
}
@end

NOTE: You can create A0Lock in any other class, even in your AppDelegate, the only requirement is that you keep it in a strong reference.

Register Native Authentication Handlers

  1. In your AppDelegate method application:didFinishLaunchingWithOptions:, add the following lines:
A0Lock *lock = [[MyApplication sharedInstance] lock];
[lock applicationLaunchedWithOptions:launchOptions];
  1. Add the following import:
#import <Lock/Lock.h>
#import “MyApplication.h"
  1. To allow native logins using other iOS apps (e.g: Twitter, Facebook, Safari etc.), add the following method:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    A0Lock *lock = [[MyApplication sharedInstance] lock];
    return [lock handleURL:url sourceApplication:sourceApplication];
}

NOTE: If you need Facebook or Twitter native authentication, continue reading to learn how to configure these. Otherwise, skip to Implement the login.

Configuration

Before continuing to configure either Facebook or Twitter integration, check that you have enabled and correctly configured the social connection with your credentials in the Dashboard.

Facebook

Lock uses the native Facebook SDK to obtain the user's access token. Use the information from your app that is configured for Facebook connections in the Auth0 dashboard:

  1. Add the following entries to the Info.plist:
Key Value
FacebookAppID YOUR_FACEBOOK_APP_ID
FacebookDisplayName YOUR_FACEBOOK_DISPLAY_NAME
  1. Register a custom URL Type with the format fb<FacebookAppID>.

NOTE: For more information on how to configure your app for Facebook, see: Facebook Getting Started Guide and Connect your app to Facebook.

Here is an example of how the entries should look:

FB plist

  1. Add the following key to the Info.plist inside the main <dict> key. To open this file in Source Code mode within Xcode, Control-Click (or right click) on it, and select Open As > Source Code.
<key>LSApplicationQueriesSchemes</key>
<array>
        <string>fbapi</string>
        <string>fb-messenger-api</string>
        <string>fbauth2</string>
        <string>fbshareextension</string>
</array>

NOTE: These entries enable compatibility with iOS 9. You can get more information about this on Facebook's developer portal at: Preparing your apps for iOS 9.

  1. Add Lock Facebook's Pod:

pod 'Lock-Facebook', '~> 2.0'

  1. Add to the import:

#import <Lock-Facebook/A0FacebookAuthenticator.h>

  1. Register it after initializing A0Lock:
A0FacebookAuthenticator *facebook = [A0FacebookAuthenticator newAuthenticatorWithDefaultPermissions];
[lock registerAuthenticators:@[facebook]];

Twitter

  1. Add Lock Twitter's Pod

pod 'Lock-Twitter', '~> 1.1'

  1. Add to the import:

#import <Lock-Twitter/A0TwitterAuthenticator.h>

  1. Configure Auth0 Twitter authenticator after you initialize A0Lock:
NSString *twitterApiKey = ... //Remember to obfuscate your api key
NSString *twitterApiSecret = ... //Remember to obfuscate your api secret
A0TwitterAuthenticator *twitter = [A0TwitterAuthenticator newAuthenticatorWithKey:twitterApiKey andSecret:twitterApiSecret];
[lock registerAuthenticators:@[twitter]];

NOTE: For more information on configuring your app for Twitter, see: Connect your app to Twitter.

Implement the login

Now you are ready to implement the Login using Lock. You only need to instantiate and present it from any of your UIViewControllers, like this:

A0Lock *lock = [[MyApplication sharedInstance] lock];
A0LockViewController *controller = [lock newLockViewController];
controller.onAuthenticationBlock = ^(A0UserProfile *profile, A0Token *token) {
    // Do something with token & profile. e.g.: save them.
    // And dismiss the ViewController
    [self dismissViewControllerAnimated:YES completion:nil];
};
[self presentViewController:controller animated:YES completion:nil];

Lock.png

Login UI Options

There are multiple ways of implementing the login box. What you see above is the Login Widget. You can also Build your own UI, or implement one of the passwordless Login Widgets: SMS or TouchID.

On successful authentication, onAuthenticationBlock will yield the user's profile and tokens.

NOTE: To learn how to save and manage the tokens and profile, see: Lock iOS: Saving and Refreshing JWT Tokens.

5. Showing user information

After the user has logged in, you can use the profile object which contains all the user information:

  self.usernameLabel.text = profile.name;
  self.emailLabel.text = profile.email;

Additional Information

See User Profile to find out all of the available properties of the user profile.

Also see: A0UserProfile.

You can also download a sample project that shows how to store/update your user profile with Auth0.

Use Auth0 for FREECreate free Account