iOS Xcode 8.3 Facebook SDK 登录错误

标签 ios swift xcode facebook facebook-login

最近每次我尝试使用 Facebook 登录时,它都不起作用,并且显示以下错误。这发生在 iOS 模拟器和实际设备上。

2017-04-27 10:18:04.361 Colour Confuse[43796:1851249] -canOpenURL: failed for URL: "fbauth2:/" - error: "The operation couldn’t be completed. (OSStatus error -10814.)" 2017-04-27 10:18:04.362 Colour Confuse[43796:1851249] Falling back to storing access token in NSUserDefaults because of simulator bug 2017-04-27 10:18:04.363 Colour Confuse[43796:1851249] -canOpenURL: failed for URL: "fbauth2:/" - error: "The operation couldn’t be completed. (OSStatus error -10814.)"

它显示 ViewController 允许用户登录,但是当我单击继续时,它会抛出以下错误,然后 View 变为空白。如果我然后按“完成”关闭 FB View Controller ,它会使应用程序完全崩溃。

Facebook login was cancelled by user. 2017-04-27 10:18:07.346 Colour Confuse[43796:1851249] -canOpenURL: failed for URL: "fbauth2:/" - error: "The operation couldn’t be completed. (OSStatus error -10814.)" 2017-04-27 10:18:07.346 Colour Confuse[43796:1851249] Falling back to storing access token in NSUserDefaults because of simulator bug 2017-04-27 10:18:07.347 Colour Confuse[43796:1851249] -canOpenURL: failed for URL: "fbauth2:/" - error: "The operation couldn’t be completed. (OSStatus error -10814.)" 2017-04-27 10:18:07.876 Colour Confuse[43796:1851249] Warning: Attempt to present <FBSDKContainerViewController: 0x7fe74da048c0> on <Colour_Confuse.GameViewController: 0x7fe74db02d80> whose view is not in the window hierarchy!

直到几天前它都工作正常,我只是不知道为什么它突然停止工作,因为我没有改变任何东西。

let facebookLogin = FBSDKLoginManager()
facebookLogin.logIn(withReadPermissions: ["email", "user_friends", "public_profile"], from: self.view?.window?.rootViewController) { (result, error) in
        if result?.isCancelled == true {
            self.fbLoginError()
        } else if error == nil {
            let credential = FIRFacebookAuthProvider.credential(withAccessToken: (result?.token.tokenString)!)
            FIRAuth.auth()?.signIn(with: credential, completion: { (user: FIRUser?, error:Error?) in
                if error == nil {
                    print("FB and Firebase Login Successful")

                    })
                } else {
                    print(error)
                    self.fbLoginError()
                }
            })
        } else {
            print(error)
            self.fbLoginError()
        }
    }

应用程序委托(delegate)

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

    FBSDKProfile.enableUpdates(onAccessTokenChange: true)

    FIRApp.configure()

    FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)

    return true
}


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


    func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

    FBSDKAppEvents.activateApp()

}

它永远不会命中任何 if 语句,只是忽略完成闭包中的所有内容。

我还启用了钥匙串(keychain)共享

PLIST

PLIST

最佳答案

尝试这样!

Appdelegate.swift

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
      FIRApp.configure()
    FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
  }

 func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    let handled = FBSDKApplicationDelegate.sharedInstance().application(app, open: url, options: options)
    return handled
}
 func applicationDidBecomeActive(_ application: UIApplication) { 
    FBSDKAppEvents.activateApp()
 }

ViewController.swift

  @IBAction func facebookLogin(sender: UIButton) {
    let fbLoginManager = FBSDKLoginManager()
    fbLoginManager.logIn(withReadPermissions: ["public_profile", "email"], from: self) { (result, error) in
        if let error = error {
            print("Failed to login: \(error.localizedDescription)")
            return
        }

        guard let accessToken = FBSDKAccessToken.current() else {
            print("Failed to get access token")
            return
        }

        let credential = FIRFacebookAuthProvider.credential(withAccessToken: accessToken.tokenString)

        // Perform login by calling Firebase APIs
        FIRAuth.auth()?.signIn(with: credential, completion: { (user, error) in
            if let error = error {
                print("Login error: \(error.localizedDescription)")
                let alertController = UIAlertController(title: "Login Error", message: error.localizedDescription, preferredStyle: .alert)
                let okayAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
                alertController.addAction(okayAction)
                self.present(alertController, animated: true, completion: nil)

                return
            }

            // Present the main view
            if let viewController = self.storyboard?.instantiateViewController(withIdentifier: "MainView") {
                UIApplication.shared.keyWindow?.rootViewController = viewController
                self.dismiss(animated: true, completion: nil)
            }

        })
    }   
}

Info.plist 插入以下 XML 片段

 <key>CFBundleURLTypes</key>
<array>
  <dict>
    <key>CFBundleURLSchemes</key>
    <array>
      <string>fb1249988161716400</string>
    </array>
  </dict>
</array>
<key>FacebookAppID</key>
<string>1249988161716400</string>
<key>FacebookDisplayName</key>
<string>YourString</string>
<key>LSApplicationQueriesSchemes</key>
<array>
  <string>fbapi</string>
  <string>fb-messenger-api</string>
  <string>fbauth2</string>
  <string>fbshareextension</string>
</array>

关于iOS Xcode 8.3 Facebook SDK 登录错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43653757/

相关文章:

c++ - 如何使用 xcode 制作简单的 OpenGl C++ 程序

ios - 无法设置 MPMusicPlayerController.shuffleMode

iOS8 View 很好,但在 iOS7 上较小

ios - 我应该继承 UICollectionViewLayout 或 UICollectionViewFlowLayout 什么

ios - 无法在 Xcode 中为启动屏幕设置自动布局约束

ios - UIViewController 覆盖

ios - 无法使用类型为 'enumerateObjects' 的参数列表调用 '((AnyObject!, NSInteger, UnsafeMutablePointer<ObjCBool>) -> ())'

ios - 在 iOS swift 5 中获取 LAN 上的设备列表及其主机名和 IP 地址

ios - 未找到 FBSDKShareKit/FBSDKShareKit.h 文件

ios - 如何以编程方式将 iOS View 连接到处理程序