title | name | alias | language | hybrid | image | tags | snippets | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
iOS Swift Tutorial |
iOS - Swift |
|
|
false |
/media/platforms/ios.png |
|
|
<%= include('../_includes/package', { pkgRepo: 'native-mobile-samples', pkgBranch: 'master', pkgPath: 'iOS/basic-sample-swift', pkgFilePath: 'iOS/basic-sample-swift/SwiftSample/Info.plist' + account.clientParam, pkgType: 'replace' }) %>
Otherwise, if you already have an existing application, please follow the steps below.
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
Add the following to the Podfile
and run pod install
:
${snippet(meta.snippets.dependencies)}
If you need help installing CocoaPods, please check this guide
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.
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.
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.
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 #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
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:
Then add Lock Facebook's Pod
pod 'Lock-Facebook', '~> 2.0'
Click on the Pods
project and select Lock-Facebook
target and set Allow Non-modular Includes In Framework Modules
to Yes:
After that, where you initialize A0Lock
, import LockFacebook
module
import LockFacebook
And register it with A0Lock
:
let facebook = A0FacebookAuthenticator.newAuthenticatorWithDefaultPermissions()
lock.registerAuthenticators([facebook])
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])
}
For more information on how to configure this, please check Obtaining Consumer and Secret Keys for Twitter.
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:
${snippet(meta.snippets.use)}
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
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.
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