Lock iOS: Using Swift

Lock was written in Objective-C but it can be used from a pure Swift project or a Hybrid project (Swift & Objective-C).

This document covers an out-of-date version of Lock for iOS - version 1. We recommend using the latest version of the library. To do so select v2 at the dropdown. If you are already using v1 but interested in upgrading, take a look at the Lock v1 to Lock v2 migration guide.

Create Objective-C Bridging Header

In order to use Lock classes in any Swift file, you need to add a Objective-C Bridging Header to your project. The easiest way is to create a dummy Objective-C file in your Swift project (or Swift file in a Objective-C project), this will make Xcode prompt to create the bridging header, just press "YES". After that you can delete the dummy file from your project and open the bridging header file which is called <YouAppName>-Bridging-Header.h.

For more information, please check this guide from Apple.

Use Lock

In <YouAppName>-Bridging-Header.h just add the following line

#import <Lock/Lock.h>

And Lock classes will be available in all your Swift codebase. So to show A0LockViewController just use the following snippet:

let authController: A0LockViewController = A0Lock.shared().newLockViewController()
authController.onAuthenticationBlock = { (profile, token) in
    guard let profile = profile, let token = token else {
        return //it's a sign up
    }

    let keychain = A0SimpleKeychain(service: "Auth0")
    keychain.setString(token.idToken, forKey: "id_token")
    if let refreshToken = token.refreshToken {
        keychain.setString(refreshToken, forKey: "refresh_token")
    }
    keychain.setData(NSKeyedArchiver.archivedData(withRootObject: profile), forKey: "profile")

    // Other stuff

    self.dismiss(animated: true, completion: nil)
}
self.present(authController, animated: true, completion: nil)