我无法调用帐户工具包的委托方法。我可能错了,但我认为这是因为委托方法没有正确地从objective-c转换成swift。下面是代码示例:

import AccountKit
import UIKit

class LoginViewController: UIViewController, AKFViewControllerDelegate {

    let accountKit = AKFAccountKit(responseType: .AuthorizationCode)
    var pendingLoginViewController: UIViewController?

    override func viewDidLoad() {
        super.viewDidLoad()
        pendingLoginViewController = accountKit.viewControllerForLoginResume()
    }

    @IBAction func loginWithEmail(sender: AnyObject) {
        let viewController = accountKit.viewControllerForEmailLoginWithEmail(nil, state: generateState()) as! AKFViewController
        viewController.enableSendToFacebook = true
        viewController.delegate = self
        presentViewController(viewController as! UIViewController, animated: true, completion: nil)
    }

    private func generateState() -> String {
        let uuid = NSUUID().UUIDString
        let indexOfDash = uuid.rangeOfString("-")!.startIndex
        return uuid.substringToIndex(indexOfDash)
    }

    func viewController(viewController: UIViewController!, didCompleteLoginWithAuthorizationCode code: String!, state: String!) {
        // This function doesn't get called when user finished their login
    }
}

最佳答案

尝试另外实现其他3个委托方法。我很快就试过了。为我调用失败回调(3)。这很好,因为我没有完成正确的sdk初始化。可能是您遇到了同样的情况,所以您查看控制器接收失败回调。由于未实现该回调,因此您不知道它:

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

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

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

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

10-06 12:15