Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't retrieve FBSDKAccessToken current token correctly in swift 3 / FBSDK 4.17

I was using the function FBSDKAccessToken.currentAccessToken() to retrieve the FB Token, which was working correctly, until I've migrated my app in swift 3 and the 4.17 SDK. Now, the function has been renamed to FBSDKAccessToken.current() and is nil when the app delegate is reloading. I've performed a few tests, and I managed to get the token after I've restarted my app and I've already logged in FB previously, but that's not the behavior I was expecting.

EDIT : I reverted to 4.15 and it's reworking again.

Here is the code in my AppDelegate :

func applicationDidBecomeActive(_ application: UIApplication)
{

    FBSDKAppEvents.activateApp();

}

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any)  -> (Bool)
{

    let wasHandled: Bool = FBSDKApplicationDelegate.sharedInstance().application(application
        ,open:url
        ,sourceApplication:sourceApplication
        ,annotation:annotation)

    if (wasHandled) {
        if let fbtoken = FBSDKAccessToken.current() {
          loginWithFacebook(fbtoken.tokenString);
        }else{
            print("no current token")
        }
    }

    return wasHandled
}

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {


    let wasHandled: Bool =  FBSDKApplicationDelegate.sharedInstance().application(
        app,
        open: url as URL!,
        sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String,
        annotation: options[UIApplicationOpenURLOptionsKey.annotation]
    )

    if (wasHandled) {
        if let fbtoken = FBSDKAccessToken.current() {
            loginWithFacebook(fbtoken.tokenString);
        }else{
            print("no current token")
        }
    }

    return wasHandled;
}

Thanks for your help :) Denis

like image 770
Denis Laboureyras Avatar asked Nov 22 '16 18:11

Denis Laboureyras


1 Answers

For some reason, this is not working anymore.

If you are using the FBSDKLoginButton you have to use FBSDKLoginButtonDelegate to wait the callback.

class LoginView: UIViewController, FBSDKLoginButtonDelegate {
    @IBOutlet weak var facebookButton: FBSDKLoginButton!

    override func viewDidLoad() {
        facebookButton.delegate = self
    }

   //Called at the end of the user connection 
   func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
       if ((error) != nil) {
           //Process error
       } else if result.isCancelled {
           // Handle cancellations
           print("result is canceled")
        } else {
            if let fbToken = result.token.tokenString {
                print(fbToken)
            }
        }
    }

    func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
        print("logout from FB")
    }
}

Alternately, you can use the FBSDKLoginManager too. Check out the documation here :

https://developers.facebook.com/docs/reference/ios/current/class/FBSDKLoginManager/

Hope this help !

like image 59
Makaille Avatar answered Nov 16 '22 18:11

Makaille