Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save michael-martinez/75a464a1719093a66e728094bd34457b to your computer and use it in GitHub Desktop.
Save michael-martinez/75a464a1719093a66e728094bd34457b to your computer and use it in GitHub Desktop.
Facebook Account Kit API Wrapper Class
//
// AccountKitBaseViewController.swift
//
// Created by Michael Martinez on 29/12/2017.
// Copyright © 2017 Michael Martinez. All rights reserved.
//
import Foundation
import AccountKit
//MARK: - Protocol
protocol AccountKitBaseViewControllerDelegate: class {
func accountKitOnLoginSuccess(accountID: String, email: String)
func accountKitOnLoginFailure(error: Error)
}
//MARK: - AccountKitBaseViewController
class AccountKitBaseViewController: UIViewController {
/// AccountKit Connection Handling Events
public var delegate: AccountKitBaseViewControllerDelegate?
/// Whether user is connected to AccountKit or not
public var isUserLoggedIn: Bool = false
private var _accountKit: AKFAccountKit!
private var _pendingLoginViewController: AKFViewController?
override func viewDidLoad() {
super.viewDidLoad()
if _accountKit == nil {
_accountKit = AKFAccountKit.init(responseType: .accessToken)
}
_pendingLoginViewController = _accountKit?.viewControllerForLoginResume()
_pendingLoginViewController?.delegate = self
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if _accountKit.currentAccessToken != nil { // already logged in
requestAccount()
} else if (_pendingLoginViewController != nil) { // Resume pending login if any
self.isUserLoggedIn = false
self.present(_pendingLoginViewController as! UIViewController, animated: animated, completion: nil)
_pendingLoginViewController = nil
} else {
self.isUserLoggedIn = false
}
}
public func accountKitLogin(email: String?, completion: (() -> Swift.Void)? = nil) {
guard isUserLoggedIn == false else { return }
guard let email = email else { return }
let inputState = NSUUID(uuidString: "ARFactory")?.uuidString
let viewController = _accountKit?.viewControllerForEmailLogin(withEmail: email, state: inputState)
viewController?.delegate = self
self.present(viewController!, animated: true, completion: nil)
completion?()
}
public func accountKitLogout(completion: (() -> Swift.Void)? = nil) {
guard isUserLoggedIn == true else { return }
isUserLoggedIn = false
_accountKit?.logOut()
completion?()
}
//MARK: - Internal
private func requestAccount() {
_accountKit?.requestAccount { (account, error) in
guard error == nil, let account = account else {
self.delegate?.accountKitOnLoginFailure(error: error!)
return
}
self.isUserLoggedIn = true
self.delegate?.accountKitOnLoginSuccess(accountID: account.accountID, email: account.emailAddress ?? "")
}
}
}
//MARK: - AKFViewControllerDelegate
extension AccountKitBaseViewController: AKFViewControllerDelegate {
func viewController(_ viewController: (UIViewController & AKFViewController)!, didCompleteLoginWith accessToken: AKFAccessToken!, state: String!) {
_accountKit = AKFAccountKit(responseType: .accessToken)
requestAccount()
}
func viewController(_ viewController: (UIViewController & AKFViewController)!, didFailWithError error: Error!) {
self.isUserLoggedIn = false
self.delegate?.accountKitOnLoginFailure(error: error)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment