2

I'm following this tutorial to integrate Facebook AccountKit with Swift. Almost everything is working but delegate functions are not called and I don't know why this is no happening.

My code:

import UIKit
import AccountKit

class LoginViewController: UIViewController,AKFViewControllerDelegate {

    //
    var _accountKit: AKFAccountKit?
    var _pendingLoginViewController: AKFViewController?
    var _authorizationCode: String?

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

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

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

    @IBAction func loginWithPhone(_ sender: UIButton) {
        let inputState = NSUUID().uuidString
        let vc: AKFViewController = _accountKit!.viewControllerForPhoneLogin(with: nil, state: inputState)
        self.prepareLoginViewController(loginViewController: vc)
        self.present(vc as! UIViewController, animated: true, completion: nil)

    }


    private func viewController(viewController: UIViewController!, didCompleteLoginWithAuthorizationCode code: String!, state: String!) {
        print("1")
    }

    private func viewController(viewController: UIViewController!, didCompleteLoginWithAccessToken accessToken: AKFAccessToken!, state: String!) {
        print("2")
    }

    private func viewController(viewController: UIViewController!, didFailWithError error: Error!) {
        print("3")
    }

    private func viewControllerDidCancel(viewController: UIViewController!) {
        print("4")
    }

}

Swift version: 3.2

I belice is something to do with the swift version (I believe this tutorial is for 2.x) but i had a similar code from an example in Swift 3 and is working. So I don't know what is going wrong.

exsnake
  • 1,767
  • 2
  • 23
  • 44

2 Answers2

4

You don't need to cahnge methods to private.

Change delegate methods to the following:

func viewController(_ viewController: (UIViewController & AKFViewController)!, 
didCompleteLoginWith accessToken: AKFAccessToken!, state: String!) {
}

func viewController(_ viewController: (UIViewController & AKFViewController)!, didCompleteLoginWithAuthorizationCode code: String!, state: String!) {

}

func viewController(_ viewController: (UIViewController & AKFViewController)!, didFailWithError error: Error!) {

}

func viewControllerDidCancel(_ viewController: (UIViewController & AKFViewController)!) {
    print("canceled")
}
  • 1
    This wasn't initially working for me, but i realized my view controller that was presenting the AccountKit view controller was getting deinitialized so there was nothing for AccountKit to call back to. Once i solved that, this above code worked without any errors or warnings. – AlBeebe Feb 08 '18 at 19:09
0

Problem might be that your delegate functions are all private and so the AKFViewController is unable to invoke them. Try making them public and see if you still can't get the callbacks

Pouya Larjani
  • 408
  • 3
  • 8