Search code examples
iosswiftfacebookhttp-redirectfacebook-ios-sdk

Handler does not get called after Facebook Browser Login redirects to Application


I am using Facebook SDK v4.8.0 for iOS in my application for login with Facebook. (iPhone OS version 9.3)

Following is some related code for integration:

AppDelegate.swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
    return true
}

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

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

func application(application: UIApplication, openURL url: NSURL, options: [String: AnyObject]) -> Bool {
        return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url,
                                                                     sourceApplication: "UIApplicationOpenURLOptionsSourceApplicationKey",
                                                                     annotation: "UIApplicationOpenURLOptionsAnnotationKey")
}

Login.Swift

@IBAction func btnFacebook(sender: UIButton) {

    self.loginToFacebookWithSuccess({ (response) -> Void in
        print("Success")
    }) { (error) -> Void in
        print("failure")
    }

func loginToFacebookWithSuccess(successBlock: () -> (), andFailure failureBlock: (NSError?) -> ()) {

    let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
    fbLoginManager.loginBehavior = .SystemAccount

    FBSDKLoginManager().logInWithReadPermissions(["public_profile", "email", "user_friends"], fromViewController: self, handler: { (result:FBSDKLoginManagerLoginResult!, error:NSError!) -> Void in

        if error != nil {
            print("error")
        } else if result.isCancelled {
            print("cancelled")
        } else {
            print("worked fine") // get Facebook user data here
        }
    })
}

When the user has not configured Facebook account in the device, the App goes to browser and provides Facebook login UI. Once, the user is done with login the Browser redirects to the App. Till this point everything works fine.

Problem

When the browser redirects the user to App, nothing happens. (No handler for success OR failure is called). Just in case of user taps on "Done" in the browser, "cancelled" is printed in log. But, for "cancel" or "okay" (or say "Continue as Username") nothing happens after redirection.

Any clue?

Let me know, if I have missed any information.


Solution

  • I think your open url method needs following change:

    func application(_ application: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey: Any]) -> Bool {
    
        let isFBURL = ((url.scheme?.hasPrefix("fb\(FBSDKSettings.appID()!)"))! && url.host == "authorize")
    
        if  isFBURL == true {
    
            return FBSDKApplicationDelegate.sharedInstance().application(application, open: url,
                                                                         sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String,
                                                                         annotation: options[UIApplicationOpenURLOptionsKey.annotation] as? String)
        }
    }
    

    Let me know, if it works.