Skip to content

Latest commit

 

History

History
258 lines (188 loc) · 8.64 KB

ios-swift.md

File metadata and controls

258 lines (188 loc) · 8.64 KB
lodash title name hybrid image tags
true
iOS Swift Tutorial
iOS - Swift
false
//auth0.com/lib/platforms-collection/img/ios.png
quickstart

iOS Swift 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:

pod 'Lock', '~> 1.12'
pod 'JWTDecode', '~> 0.2'

If you need help installing CocoaPods, please check this guide

2. Configuring your Swift project to use an ObjC library

Since CocoaPods 0.36 you can build any library as a Cocoa Touch framework. This allows to import them directly in your swift files like this:

import Lock

To enable this feature, just add this line at the top level of your Podfile (outside any target definition):

use_frameworks!

If you dont want to use this feature please read this guide.

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

import UIKit
import Lock

class MyApplication: NSObject {
    static let sharedInstance = MyApplication()
    let lock: A0Lock
    private override init() {
        lock = A0Lock()
    }
}

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

4. Register Native Authentication Handlers

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

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

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
    let 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

IMPORTANT: Before you continue to the next section, 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'

After that, where you initialize A0Lock, import LockFacebook module

import LockFacebook

And register it with A0Lock:

let facebook = A0FacebookAuthenticator.newAuthenticatorWithDefaultPermissions()
lock.registerAuthenticators([facebook])

####Twitter

First add Lock Twitter's Pod

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

After that, where you initialize A0Lock, import LockTwitter module

import LockTwitter

And register it with A0Lock:

let apiKey = ... //Remember to obfuscate your api key
let apiSecret = ... //Remember to obfuscate your api secret
let twitter = A0TwitterAuthenticator.newAuthenticationWithKey(apiKey, andSecret:apiSecret)
lock.registerAuthenticators([twitter])
}

5. Let's implement the login

Now we're ready to implement the Login. We can instantiate A0LockController and present it as a modal screen. In one of your controllers instantiate the native widget and present it as a modal screen:

let lock = MyApplication.sharedInstance.lock
let controller = lock.newLockViewController()
controller.closable = true
controller.onAuthenticationBlock = {(profile:A0UserProfile!, token:A0Token!) -> () in
  // Do something with token & profile. e.g.: save them.
  // Lock will not save the Token and the profile for you.
  // And dismiss the ViewController
  self.dismissViewControllerAnimated(true, completion: nil)
}
self.presentViewController(controller, animated: true, completion: nil)

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

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

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