简体   繁体   中英

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

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:列表

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)
}

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 = FBSDKLoginManager();
fbLoginManager.loginBehavior = FBSDKLoginBehaviorSystemAccount;
// call login method of choice here

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.

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 had very similar problem while adding LoginButton in my app by following this tutorial → https://developers.facebook.com/docs/swift/login .

After clicking the Facebook Connect button and finishing the authorization, it always returns with status 'cancelled'. I tried many solutions and finally the following worked:

In AppDelegate.swift , add the following 2 functions:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    return SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
}

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

I got this issue too. It turned out that the app bundle id (in project configuration) mismatched with the app bundle id configured in the facebook app dashboard. After fixing the bundle id, the login worked just fine.

Hope this helps someone.

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 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

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.

UPDATE 2020-10-09:

FBSDK version 4.38.0 uses UIWebView, which is currently being discontinued by Apple and will be completely discontinued in December.

ORIGINAL:

I've been having the same problem and none of the other answers here worked for me. I went through the Facebook documentation carefully to make sure that I had everything correct, which I did. After some research, I discovered that this is a problem within the FBSDK itself starting with version 4.39.0. As it turned out, I had been using FBSDK version 4.40.0. The problem was completely resolved when I reverted to FBSDK version 4.38.0.

Here's a link which explains how to revert to FBSDK version 4.38.0 if you are using cocoapods

If however you are like me and prefer not to use cocoapods, here is a link to download FBSDK version 4.38.0

13 january 2019

Solved for me :

Delete Podfile.lock
Delete Pods folder
replace the facebook pods in your Podfile with this version of facebook

pod 'FBSDKCoreKit', '~> 4.38.0'
pod 'FBSDKLoginKit', '~> 4.38.0'

now please notice that if you have FacebookShareKit or anything else for facebook you have to change it to the same version you are using which is 4.38.0

run your terminal with pod install
open your project, go to Product > Clean Build Folder
run the app and try.

worked for me, if it does for you let everybody knows.

original post: https://github.com/facebookarchive/facebook-swift-sdk/issues/298#issuecomment-449626683

cheers.

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
}

In my case that using React-Native, FacebookSDK and Deeplink together, I need to have only one openUrl inside AppDelegate.m. More details explanation is in https://github.com/facebook/react-native-fbsdk/blob/master/README.md#32-ios-project

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