Facebook login fails (always isCancelled) after upgrading to SDK 4.1

橙三吉。 提交于 2020-01-03 10:54:14

问题


I have upgraded Facebook SDK from 3.21.1 to 4.1 in an iOS app (already live).

I followed carefully the upgrade guide, and implemented the new methods for login. The code I used is the one provided in Facebook documentation.

But since the upgrade, each time I try to log in (device or simulator, webview or Facebook app), I can go through the login flow successfully, but when I fall back on my app, the login does not return any error but returns a FBSDKLoginManagerLoginResult "isCancelled".

As a workaround, I tried to implement app invites, which do not require login, but I'm stuck with a "Attempting to authenticate in FB" in the console... No luck here either.

So I guess it has something to do with the authentication fallback and the URL scheme in the info.plist, but I double checked there, and the data (which was working fine before the upgrade) is the same as the one indicated in Facebook documentation.

Anyone has any clue?? Thanks!

What I already checked:

  • I did not change the info.plist which was already set up to use Facebook SDK and worked fine before the upgrade.
  • The user account I use for login also worked fine before this upgrade.
  • I do not have any currentAccessToken before or after Login process.

回答1:


Thanks to Dheeraj and its answer to a similar question, I found the error in my calls. I just made the stupidest error in Swift. In the unlikely event someone as dumb as I am read this, here are the 3 calls in Swift that you should add in your AppDelegate.swift. They are tested and working:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    // Do what you have to do but at the end, instead of 'return true', put :

    return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
    return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}

func applicationDidBecomeActive(application: UIApplication) {
    FBSDKAppEvents.activateApp()
}



回答2:


Thumbs up for your help winterized. And thanks Dheeraj bhai. Here I'm just updating the answer for Swift 3.1.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}

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

func applicationDidBecomeActive(application: UIApplication) {
    FBSDKAppEvents.activateApp()
}

And yes don't forgot to put:

import FBSDKCoreKit

in App Delegate.




回答3:


Another reason for the call to FBSDKLoginManagerLoginResult to always return isCancelled: if the app (in development) is asking for certain permissions, the user is not a listed developer for this app, and FaceBook has not yet approved the app for these permissions, it will always return isCancelled.




回答4:


I found the solution, the problem is that you don't complete the Facebook login process.

when you make facebook login request and then the user uses login via facebook app, The Facebook app opens again your application and call to

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

on app delegate, to complete the process you need to pass facebook SDK the URL that came from Facebook that way:

  func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
        return FBSDKApplicationDelegate.sharedInstance().application(app, open: url, options: options) == true {
    }

Then you will see that the login request will return access token and won't be canceled.



来源:https://stackoverflow.com/questions/30479600/facebook-login-fails-always-iscancelled-after-upgrading-to-sdk-4-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!