简体   繁体   中英

Facebook SDK 4.0 - currentAccessToken still nil within loginButton didCompleteWithResult

I am upgrading to Facebook SDK 4.X and I am having trouble determining what to do immediately after login.

In the Facebook Delegate's loginButton didCompleteWithResult method, it returns successfully (skips the error if statement), so for my system I need to register the user with my server, so in the previous version of the Facebook SDK, I would send the access token to my server.

But in this didCompleteWithResult method, [FBSDKAccessToken currentAccessToken] is still returning nil

all my AppDelegate lifecycle methods return the facebook functions, so I'm not sure what else could be happening incorrectly, or where I should expect the AccessToken to be populated

You probably forgot the call the FBSDKApplicationDelegate in the didFinishLaunchingWithOptions method.

See the answer here: https://stackoverflow.com/a/30406194/561485 .

The documentation explains more about the application:didFinishLaunchingWithOptions: method.

In my case I was facing this issue as I was using another Google sign in as well. So, the url wasn't handled by facebook sdk. I had to change the app delegate methods as follows:

 @available(iOS 9.0, *)
func application(_ application: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any])
    -> Bool {
       let gHandle = GIDSignIn.sharedInstance().handle(url,
                                                 sourceApplication:options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String,
                                                 annotation: [:])
        let fHandle = FBSDKApplicationDelegate.sharedInstance().application(application, open: url, options: options)
        return gHandle || fHandle

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM