Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.
like image 586
winterized Avatar asked May 27 '15 10:05

winterized


2 Answers

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()
}
like image 145
winterized Avatar answered Oct 05 '22 02:10

winterized


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.

like image 32
TalBenAsulo Avatar answered Oct 05 '22 02:10

TalBenAsulo