Skip to content

Latest commit

 

History

History
221 lines (160 loc) · 7.66 KB

ios-objc.md

File metadata and controls

221 lines (160 loc) · 7.66 KB
lodash title name hybrid alias language image tags snippets
true
iOS Objective-C Tutorial
iOS - Objective C
false
objective-c
iphone
ipad
Objective C
//auth0.com/lib/platforms-collection/img/ios.png
quickstart
dependencies setup use
native-platforms/ios-objc/dependencies
native-platforms/ios-objc/setup
native-platforms/ios-objc/use

iOS Objective-C Tutorial

<% if (configuration.api && configuration.thirdParty) { %>

<% } else { %>

<% } %>

Otherwise, if you already have an existing application, please follow the steps below.

Before Starting

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

a0@@account.clientId@@://\*.auth0.com/authorize

1. Adding the Auth0 dependencies

Add the following to the Podfile and run pod install:

@@snippet(meta.snippets.dependencies)@@

If you need help installing CocoaPods, please check this guide

2. Configure Auth0 Lock for iOS

Add the following entries to your app's Info.plist:

Key Value
Auth0ClientId @@account.clientId@@
Auth0Domain @@account.namespace@@

Also you'll need to register a new URL Type with the following scheme a0@@account.clientId@@. You can do it from your app's target Info section.

Url type register

The next step is to create and configure an instance of A0Lock with your Auth0 credentials from Info.plist. We are going to do this in a custom object called MyApplication.

@@snippet(meta.snippets.setup)@@

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

3. Register Native Authentication Handlers

First in your AppDelegate method application:didFinishLaunchingWithOptions: add the following lines:

A0Lock *lock = [[MyApplication sharedInstance] lock];
[lock applicationLaunchedWithOptions:launchOptions];

Then to allow native logins using other iOS apps, e.g: Twitter, Facebook, Safari etc, you need to 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];
}

If you need Facebook or Twitter native authentication please continue reading to learn how to configure them. Otherwise please go directly to step #4

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

Facebook

Lock uses the native Facebook SDK to obtain the user's access token so you'll need to configure it using your Facebook App info:

First, add the following entries to the Info.plist:

Key Value
FacebookAppId YOUR_FACEBOOK_APP_ID
FacebookDisplayName YOUR_FACEBOOK_DISPLAY_NAME

Then, register a custom URL Type with the format fb<FacebookAppId>.

For more information on how to configure this, please check Facebook Getting Started Guide.

Note: The Facebook app should be the same as the one set in Facebook's Connection settings on your Auth0 account

Here's an example of how the entries should look like:

FB plist

Then add Lock Facebook's Pod

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

And register it after initializing A0Lock:

A0FacebookAuthenticator *facebook = [A0FacebookAuthenticator newAuthenticatorWithDefaultPermissions];
[self.lock registerAuthenticators:@[facebook]];

####Twitter

First add Lock Twitter's Pod

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

And 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 newAuthenticationWithKey:twitterApiKey andSecret:twitterApiSecret];
[self.lock registerAuthenticators:@[twitter]];
}

4. Let's implement the login

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

@@snippet(meta.snippets.setup)@@

Lock.png

Note: There are multiple ways of implementing the login box. What you see above is the Login Widget, but if you want, you can use your own UI. Or you can also try our passwordless Login Widgets: SMS or TouchID

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

To learn how to save and manage the tokens and profile, please read this guide

5. Showing user information

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

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

You can click here to find out all of the available properties from the user's profile or you can check A0UserProfile. Please note that some of this depend on the social provider being used.

6. We're done

You've implemented Login and Signup with Auth0 in iOS. You're awesome!.

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