tutorial - ios 11 facebook login




Facebook login always comes back as cancelled.(iOS Swift) (8)

I'm currently trying implement Facebook login using the 4.0 version of the SDK, this also happens with the 3.+ version. When I call logInWithReadPermissions (4.0 version) or openActiveSessionWithReadPermissions (3.+ version). The closure/block is called immediately with isCancelled (4.0 version) and ClosedFailedLogin (3.+ version) before the user can make a selection (cancel or ok). I thought it may be a problem with the URL Scheme in my plist settings but I've checked it over and over and everything seems to be right. Just wondering if anyone may have any ideas on solving this problem. My Bundle ID is right, single signin is on, native app is enabled, in the Facebook dev console. See some sample code and configurations below (4.0 version).

Login Call:

AppDelegate:

Plist:


Did you add function to AppDelegate?

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

I found that the wrong AppDelegate method was being called. It was calling

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

instead of

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

So i had to add this to the first AppDelegate method (the one that was being called):

return FBSDKApplicationDelegate.sharedInstance().application(app, open: url, sourceApplication: "com.apple.mobilesafari", annotation: nil)

Note that i had to pass the safari bundle ID explicitly, otherwise the FB account delegate would get isCancelled = true


I had the same issue. Don't mix FBSDKApplicationDelegate and SDKApplicationDelegate

Solved it with the next Swift 4 code in AppDelegate:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
    return true
}

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    let appId = SDKSettings.appId
    if url.scheme != nil && url.scheme!.hasPrefix("fb\(appId)") && url.host == "authorize" { // facebook
        return SDKApplicationDelegate.shared.application(app, open: url, options: options)
    }
    return false
}

I had the some problem but I found a workaround. You can set the login behaviour of the Login Manager to use the Facebook details on the phone. The default behaviour is FBSDKLoginBehaviorSystemNative and that tries to use the Facebook app first and then if its not there, it uses a web modal.

Instead of doing it that way and passing around urls that don't seem to work you can set the login behaviour as FBSDKLoginBehaviorSystemAccount.

Long story short, try:

let fbLoginManager = FBSDLoginManager();
fbLoginManager.loginBehavior = FBSDKLoginBehaviorSystemAccount;
// call login method of choice here

I have also faced this problem from past 1 month. I was integrating facebook login with firebase in ios 10 swift 3. Finally, I m able to successfully implement it. I corrected it by making following changes in AppDelegate file.

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

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


    @available(iOS 9.0, *)
    func application(_ application: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any])
        -> Bool {
            var shouldOpen :Bool = FBSDKApplicationDelegate.sharedInstance().application(application,  open: url, sourceApplication:options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String!,annotation: options[UIApplicationOpenURLOptionsKey.annotation])
            return shouldOpen
    }

    // for iOS below 9.0
    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        var shouldOpen :Bool = FBSDKApplicationDelegate.sharedInstance().application(application,open: url as URL!,sourceApplication: sourceApplication,annotation: annotation)
        return shouldOpen
    }

I Hope it might help others

tested on ios 10.2 Emulator, iPhone 6 Plus running with 10.2


I have same issue login method always return canacelled then I add below things in info.plist

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>akamaihd.net</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
        <key>facebook.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
        <key>fbcdn.net</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
    </dict>
</dict>
<key>LSApplicationQueriesSchemes</key>
<array>
    <string>fbapi</string>
    <string>fb-messenger-api</string>
    <string>fbauth2</string>
    <string>fbshareextension</string>
</array>

and in AppDelegate update didFinishLaunching method and add new method like below

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

    return SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)

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

Login method is given below:

@objc func loginButtonClicked() {
    let  loginManager = LoginManager()
    loginManager.loginBehavior = .systemAccount
    loginManager.logIn([ .publicProfile,.userFriends,.email ], viewController: self) { loginResult in
        switch loginResult {
        case .failed(let error):
            print(error)
        case .cancelled:
            print("User cancelled login.")
        case .success(let grantedPermissions, let declinedPermissions, let accessToken):
            print("Logged in!")
            //Do further code...
        }
    }
}

it works for me in Swift 3.0 and SDK 4.17.0, I hope it works for you Thanks.


My issue was caused from me modally presenting a loading view controller, which I had being dismissed when the view disappeared. Since FBSDK calls the viewWillDisappear method when transitioning to Safari or the app, it was closing the view and the FB login screen. Same thing can happen if you present the login over a UIAlertController.


With Facebook SDK version 4 I only had to add this to my application delegate on iOS 10 & Swift 3 to make the authentication work. Before that I also only had cancelled logins.

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




facebook-ios-sdk