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

标签 ios swift facebook authentication ios10

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

这是我的 View Controller :

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];  
  }];  
}  

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

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

关于ios - Facebook 登录 iOS 10 一直有效,直到第二次按下按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39716675/

相关文章:

ios - 使用 QtCreator 构建应用程序 iOS 时出错

swift - 将 Firebase 身份验证与 Firebase 数据库连接

php - Facebook 的 l.php

xcode - Facebook SDK 4.0.1 Swift 错误 xcode 6.2 iOS 8.2

ios - 在设备上运行 Cucumber,应用立即崩溃,错误 : "Error while writing to fifo. RunLoop::Fifo::NoReaderConfiguredError"

ios - RKObjectManager 的 deleteObject 带有 EXC_BAD_ACCESS

ios - 转换二维数组

ios - 尝试在 Xcode 中本地化项目时,XLIFF 不包含所有形式的复数形式

android - Facebook 授权访问 token ?

ios - 如何使 UISlider 匹配音频进度