How to integrate Facebook’s Account Kit [Quick setup] [Swift]

Ro Higuera
2 min readApr 18, 2016

Hey! I decided to write this short tutorial cause today (April 17th, 2016), I struggled a lot with an specific part of Account Kit Integration Tutorial [iOS] and couln’t find anything helpful for an integration in Swift.

Please note that this is just a very basic Swift setup, to see the original reference please go to Facebook’s official guide and do a little bit of objc to Swift translation (:

Big shoutout to Alex who noticed you could do a AKFViewController cast and save A LOT of work!!

So after you have created a developer account and got a Facebook App ID, you need to download the Account Kit SDK.

1. Configuring your info.plist

<key>AccountKitClientToken</key>
<string>{your-account-kit-client-token}</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>ak{your-app-id}</string>
</array>
</dict>
</array>

2. Initialize Account Kit’s SDK

import AccountKitclass MyViewController: UIViewController {
var _accountKit: AKFAccountKit?
var _pendingLoginViewController: AKFViewController?
var _authorizationCode: String?
...
}

3. Initialize _accountKit variable

override func viewDidLoad() {
super.viewDidLoad()
if _accountKit == nil {
_accountKit = AKFAccountKit(responseType: .AuthorizationCode)
}
_pendingLoginViewController = _accountKit!.viewControllerForLoginResume() as? AKFViewController
_pendingLoginViewController?.delegate = self
}

4. Prepare AK’s Login VC

func prepareLoginViewController(loginViewController: AKFViewController) {
loginViewController.delegate = self
}

5. Initiate Login Flow

func signupWithEmailButtonPressed() {
let inputState = NSUUID().UUIDString
let vc: AKFViewController = _accountKit!.viewControllerForEmailLoginWithEmail(nil, state: inputState) as! AKFViewController
self.prepareLoginViewController(vc)
self.presentViewController(vc as! UIViewController, animated: true, completion: nil)
}

6. Add AKFViewControllerDelegate’s callbacks

extension MyViewController: AKFViewControllerDelegate {
func viewController(viewController: UIViewController!, didCompleteLoginWithAuthorizationCode code: String!, state: String!) {
print("did complete login with AuthCode \(code) state \(state)")
}
func viewController(viewController: UIViewController!, didFailWithError error: NSError!) {
print("error \(error)")
}
func viewController(viewController: UIViewController!, didCompleteLoginWithAccessToken accessToken: AKFAccessToken!, state: String!) {
print("did complete login with access token \(accessToken.tokenString) state \(state)")
}
}

7. Handle different login states

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if ([self isUserLoggedIn]) {
//if the user is already logged in, go to the main screen
//go to main screen
} else if (_pendingLoginViewController != nil) {
//resume pending login (if any)
self.prepareLoginViewController(_pendingLoginViewController!)
self.presentViewController(_pendingLoginViewController as! UIViewController, animated: true, completion: nil)
_pendingLoginViewController = nil;
}
}

I really hope this small tutorial saves you a lot of time! I know I invested a lot of it trying to solve some translation issues!

-Ro.

--

--