Facebook 登录 iOS 10 有效,直到第二次按下按钮

Facebook Login iOS 10 works until button is pressed for a second time

我一直在使用 Swift 3 为 iOS 10 上的 Facebook 登录创建自定义按钮。不幸的是,在我按下 UIButton 之前,Facebook 登录不会显示 SafariViewController第二次。多亏了断点,我知道它会调用 loginManager.logIn() 但直到我第二次按下 UIButton 时它才进入回调。尽管如果我将 FacebookLoginButton() 与我的自定义一起使用,一切正常。我真的不知道出了什么问题。希望您能够帮助我。谢谢!

这是我的 ViewController:

import UIKit
import FacebookCore
import FacebookLogin

class ViewController: UIViewController {

override func viewDidLoad() {

    self.view.backgroundColor = .blue

    // Add a custom login button to your app
    let myLoginButton = UIButton(type: .system)
    myLoginButton.backgroundColor = .red
    myLoginButton.frame = CGRect(x: 0, y: 0, width: 180, height: 40)
    myLoginButton.center = view.center;
    myLoginButton.setTitle("My Button", for: .normal)

    // Handle clicks on the button
    myLoginButton.addTarget(self, action: #selector(self.loginButtonClicked), for: .touchUpInside)

    // Add the button to the view
    view.addSubview(myLoginButton)
}

// Once the button is clicked, show the login dialog
func loginButtonClicked() {
    let loginManager = LoginManager()
    loginManager.logIn([.publicProfile], 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!")
        }
    }
}

还有我的 info.plist(唯一的变化是我的应用名称和 ID 的占位符:

 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
 <plist version="1.0">
 <dict>
 <key>CFBundleDevelopmentRegion</key>
 <string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
    <string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>fb[A Number]</string>
        </array>
    </dict>
</array>
<key>FacebookAppID</key>
<string>[My Facebook App ID]</string>
<key>FacebookDisplayName</key>
<string>[My App Name]</string>
<key>LSApplicationQueriesSchemes</key>
<array>
    <string>fbapi</string>
    <string>fb-messenger-api</string>
    <string>fbauth2</string>
    <string>fbshareextension</string>
</array>

更新:这已被 facebook 确认为一个错误 https://developers.facebook.com/bugs/622990254549819/is

问题与驻留在 Facebook SDK 中的方法有关

- (void)logInWithBehavior:(FBSDKLoginBehavior)loginBehavior  
{  
  __weak __typeof__(self) weakSelf = self;  
  [FBSDKServerConfigurationManager loadServerConfigurationWithCompletionBlock:^(FBSDKServerConfiguration *serverConfiguration, NSError *loadError) {  
    [weakSelf logInWithBehavior:loginBehavior serverConfiguration:serverConfiguration serverConfigurationLoadError:loadError];  
  }];  
}  

您将无法在视图中登录,因为使用包含 LoginManager init 的按钮回调,FBSDKServerConfigurationManager 在您第一次按下登录按钮时为 null,但在第二次时为 init。

我已经解决了一个非常相似的问题,只需将管理器声明为全局变量并在 viewDidLoad 中初始化 LoginManager。