Skip to content

Latest commit

 

History

History
278 lines (209 loc) · 9.19 KB

ios-swift.md

File metadata and controls

278 lines (209 loc) · 9.19 KB
title name alias language hybrid image tags snippets
iOS Swift Tutorial
iOS - Swift
ios
Swift
false
/media/platforms/ios.png
quickstart
dependencies setup use
native-platforms/ios-swift/dependencies
native-platforms/ios-swift/setup
native-platforms/ios-swift/use

iOS Swift Tutorial

::: panel-info System Requirements This tutorial and seed project have been tested with the following:

  • CocoaPods 0.39.0
  • XCode 7.2.1
  • Simulator - iOS 9.2 - iPhone 6 :::

<%= include('../_includes/_package', { pkgRepo: 'native-mobile-samples', pkgBranch: 'master', pkgPath: 'iOS/basic-sample-swift', pkgFilePath: 'iOS/basic-sample-swift/SwiftSample/Info.plist', pkgType: 'replace' }) %>

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. 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, there is a line at the top level of the 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 this in your App's Target menu, in the Info section.

Url type register

You can access an instance of A0Lock using the sharedLock() method provided by the Lock pod.

${snippet(meta.snippets.setup)}

You can access A0Lock in any class, even in your AppDelegate; the only requirement is that you reference the Lock pod by using import Lock.

4. Register Native Authentication Handlers

First, add the following lines to your AppDelegate.swift application function that contains the parameter application:didFinishLaunchingWithOptions:

A0Lock.sharedLock().applicationLaunchedWithOptions(launchOptions)

To allow native logins using other iOS apps, e.g: Twitter, Facebook, Safari etc, you need to add the following method to your AppDelegate.swift file:

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
    return A0Lock.sharedLock().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 #5

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 and Obtaining an App ID and App Secret for Facebook.

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 the following keys 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, select Open As, Source Code.

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>facebook.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>                
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
        <key>fbcdn.net</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
        <key>akamaihd.net</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
    </dict>
</dict>
<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 in Facebook's developer portal: Preparing your apps for iOS 9

Then add Lock Facebook's Pod

pod 'Lock-Facebook', '~> 2.1'
post_install do |installer|
    installer.pods_project.build_configurations.each { |bc|
        bc.build_settings['CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES'] = 'YES'
    }
end

After that, where you initialize A0Lock, import LockFacebook module

import LockFacebook

And register it with A0Lock:

let facebook = A0FacebookAuthenticator.newAuthenticatorWithDefaultPermissions()
A0Lock.sharedLock().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)
A0Lock.sharedLock().registerAuthenticators([twitter])
}

For more information on how to configure this, please check Obtaining Consumer and Secret Keys for Twitter.

5. Let's implement the Login

Now we're ready to implement the Login. We can get an instance of A0LockController by calling the newLockViewController() method of the A0Lock shared instance and present it as a modal screen. In one of your controllers, instantiate the native widget and present it as a modal screen:

${snippet(meta.snippets.use)}

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. Note that Lock on its own will not save these for you.

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 with Swift. You're awesome!

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