Thi
Thi

Reputation: 2116

Facebook Account Kit in Swift

I couldn't get the Account Kit's delegate methods to get called. I might be wrong but I think this is because of the delegate methods didn't get translated correctly from Objective-C into Swift. Here is the code samples:

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
    }
}

Upvotes: 1

Views: 2818

Answers (3)

Anandram Venkataswamy
Anandram Venkataswamy

Reputation: 13

@JayVDiyk: How do you get the user inputted phone number from the callback? –

Answer: You can get that from the accountkit object by requestAccount menthod

accountKit.requestAccount({ account, error in
                print("Account number \(account?.accountID)")
                print("Email: \(account?.emailAddress)")
                print("Phone: \(account?.phoneNumber?.stringRepresentation())")
})

Upvotes: 1

Thi
Thi

Reputation: 2116

I was wrong. I set responseType to .AuthorizationCode but was waiting for a callback on .AccessToken.

Upvotes: 1

Igor B.
Igor B.

Reputation: 2229

Try additionaly implement other 3 delegate methods. I quickly tried this. Failure callback is called for me (#3). That's fine as I did not complete proper SDK initialization. May be you experience same situation, so that you view controller receives failure callback. As that callback is not implemented you are not aware of it:

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")
}

Upvotes: 2

Related Questions