简体   繁体   English

Facebook 登录总是被取消。 (iOS Swift)

[英]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.我目前正在尝试使用 4.0 版本的 SDK 实现 Facebook 登录,3.+ 版本也会发生这种情况。 When I call logInWithReadPermissions (4.0 version) or openActiveSessionWithReadPermissions (3.+ version).当我调用logInWithReadPermissions (4.0 版)或openActiveSessionWithReadPermissions (3.+ 版)时。 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 ).在用户可以进行选择(取消确定)之前,使用isCancelled (4.0 版本)和ClosedFailedLogin (3.+ 版本)立即调用闭包/块。 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.我认为我的 plist 设置中的 URL Scheme 可能有问题,但我已经一遍又一遍地检查它,一切似乎都是正确的。 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.我的 Bundle ID 是正确的,单点登录已启用,本机应用程序已启用,在 Facebook 开发控制台中。 See some sample code and configurations below (4.0 version).请参阅下面的一些示例代码和配置(4.0 版本)。

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.使用 Facebook SDK 版本 4,我只需要将它添加到 iOS 10 和 Swift 3 上的应用程序委托中即可进行身份验证。 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.您可以设置登录管理器的登录行为以使用手机上的 Facebook 详细信息。 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.默认行为是 FBSDKLoginBehaviorSystemNative,它首先尝试使用 Facebook 应用程序,然后如果它不存在,则使用网络模式。

Instead of doing it that way and passing around urls that don't seem to work you can set the login behaviour as FBSDKLoginBehaviorSystemAccount .您可以将登录行为设置为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我有同样的问题登录方法总是返回取消然后我在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并在AppDelegate 中更新didFinishLaunching方法并添加如下新方法

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.它在Swift 3.0SDK 4.17.0 中对我有用,我希望它对你有用,谢谢。

Did you add function to AppDelegate?您是否向 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 .按照本教程→ https://developers.facebook.com/docs/swift/login在我的应用程序中添加 LoginButton 时,我遇到了非常类似的问题。

After clicking the Facebook Connect button and finishing the authorization, it always returns with status 'cancelled'.单击 Facebook Connect 按钮并完成授权后,它始终返回“已取消”状态。 I tried many solutions and finally the following worked:我尝试了很多解决方案,最终以下方法奏效:

In AppDelegate.swift , add the following 2 functions:AppDelegate.swift 中,添加以下 2 个函数:

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.结果发现应用程序包 ID(在项目配置中)与 Facebook 应用程序仪表板中配置的应用程序包 ID 不匹配。 After fixing the bundle id, the login worked just fine.修复 bundle id 后,登录工作正常。

Hope this helps someone.希望这可以帮助某人。

I found that the wrong AppDelegate method was being called.我发现调用了错误的 AppDelegate 方法。 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):所以我不得不将它添加到第一个 AppDelegate 方法(被调用的方法):

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请注意,我必须明确传递 safari 包 ID,否则 FB 帐户委托将获得isCancelled = true

I have also faced this problem from past 1 month.过去 1 个月我也遇到过这个问题。 I was integrating facebook login with firebase in ios 10 swift 3. Finally, I m able to successfully implement it.我在 ios 10 swift 3 中将 facebook 登录与 firebase 集成在一起。最后,我能够成功实现它。 I corrected it by making following changes in AppDelegate file.我通过在 AppDelegate 文件中进行以下更改来纠正它。

 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在 ios 10.2 模拟器、运行 10.2 的 iPhone 6 Plus 上测试

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.由于 FBSDK 在转换到 Safari 或应用程序时调用 viewWillDisappear 方法,因此它会关闭视图和 FB 登录屏幕。 Same thing can happen if you present the login over a UIAlertController.如果您通过 UIAlertController 显示登录信息,也会发生同样的事情。

UPDATE 2020-10-09:更新 2020-10-09:

FBSDK version 4.38.0 uses UIWebView, which is currently being discontinued by Apple and will be completely discontinued in December. FBSDK 4.38.0 版本使用 UIWebView,目前苹果已经停产,12 月份完全停产。

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.我仔细阅读了 Facebook 文档,以确保我所做的一切都是正确的。 After some research, I discovered that this is a problem within the FBSDK itself starting with version 4.39.0.经过一番研究,我发现这是 FBSDK 本身从 4.39.0 版本开始的一个问题。 As it turned out, I had been using FBSDK version 4.40.0.事实证明,我一直在使用 FBSDK 4.40.0 版。 The problem was completely resolved when I reverted to FBSDK version 4.38.0.当我恢复到 FBSDK 版本 4.38.0 时,问题完全解决。

Here's a link which explains how to revert to FBSDK version 4.38.0 if you are using cocoapods 这是一个链接,它解释了如何在使用 cocoapods 时恢复到 FBSDK 版本 4.38.0

If however you are like me and prefer not to use cocoapods, here is a link to download FBSDK version 4.38.0然而,如果你像我一样不喜欢使用 cocoapods,这里是下载 FBSDK 版本 4.38.0 的链接

13 january 2019 2019 年 1 月 13 日

Solved for me :为我解决:

Delete Podfile.lock删除 Podfile.lock
Delete Pods folder删除 Pods 文件夹
replace the facebook pods in your Podfile with this version of facebook用这个版本的 facebook替换Podfile 中的 facebook pods

pod 'FBSDKCoreKit', '~> 4.38.0' pod 'FBSDKCoreKit', '~> 4.38.0'
pod 'FBSDKLoginKit', '~> 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现在请注意,如果您有 FacebookShareKit 或其他任何用于 facebook 的东西,您必须将其更改为您正在使用的相同版本,即 4.38.0

run your terminal with pod install使用 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原帖: https : //github.com/facebookarchive/facebook-swift-sdk/issues/298#issuecomment-449626683

cheers.干杯。

I had the same issue.我遇到过同样的问题。 Don't mix FBSDKApplicationDelegate and SDKApplicationDelegate不要混合使用FBSDKApplicationDelegateSDKApplicationDelegate

Solved it with the next Swift 4 code in AppDelegate:使用 AppDelegate 中的下一个 Swift 4 代码解决了这个问题:

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.在我将 React-Native、FacebookSDK 和 Deeplink 一起使用的情况下,我只需要在 AppDelegate.m 中有一个 openUrl。 More details explanation is in https://github.com/facebook/react-native-fbsdk/blob/master/README.md#32-ios-project更多细节解释在https://github.com/facebook/react-native-fbsdk/blob/master/README.md#32-ios-project

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM