首页 文章

无法在Xcode Swift Facebook登录SDK中获取访问令牌

提问于
浏览
1

我正在尝试在我的应用中测试Facebook SDK登录但是一直收到此错误 . 我看到一个对话框,它接受我的电子邮件和密码登录,然后告诉我,我已经接受了该应用程序 . 我应该看到一条消息“已登录,但我只看到下面的错误 . 如何在Xcode iOS模拟器中测试Facebook SDK . 我在带有警报的iPhone上测试了这一点,以显示访问令牌,但结果相同 . 将始终显示“用户取消登录” .

-canOpenURL: failed for URL: "fbauth2:/" - error: The operation couldn’t be completed. (OSStatus error -10814.) User cancelled login.

这是我的代码 .

func signInWithFacebook(button: UIButton) {

    let loginManager = LoginManager()

    loginManager.logIn([ .publicProfile ], viewController: self) { loginResult in

        switch loginResult {

        case .failed(let error):

            let alertController: UIAlertController = UIAlertController(title: "Login error", message: error as? String, preferredStyle: .alert)

            let okAction: UIAlertAction = UIAlertAction(title: "OK", style: .default) { action -> Void in }

            alertController.addAction(okAction)

            self.present(alertController, animated: true, completion: nil)

            print(error)

        case .cancelled:

            let alertController: UIAlertController = UIAlertController(title: "Login cancelled", message: "User cancelled login", preferredStyle: .alert)

            let okAction: UIAlertAction = UIAlertAction(title: "OK", style: .default) { action -> Void in }

            alertController.addAction(okAction)

            self.present(alertController, animated: true, completion: nil)

            print("User cancelled login.")

        case .success(let grantedPermissions, let declinedPermissions, let accessToken):

            print(grantedPermissions)

            print(declinedPermissions)

            print(accessToken)

            let alertController: UIAlertController = UIAlertController(title: "Login success", message: String(describing: accessToken), preferredStyle: .alert)

            let okAction: UIAlertAction = UIAlertAction(title: "OK", style: .default) { action -> Void in }

            alertController.addAction(okAction)

            self.present(alertController, animated: true, completion: nil)

            print("Logged in")
        }
    }
}

在第一遍,我得到了Facebook登录对话框,但之后我只看到一个对话框,说我已经用OK按钮授权了我的应用程序 .

let accessToken = AccessToken.current

在viewDidLoad()中总是返回nil . 我需要获取accessToken,以便将其发送到API并测试响应 .

4 回答

  • 0

    我需要在AppDelegate.swift中添加此功能 . 它似乎完全没有记录 .

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

    如果您已将Info.plist中的CFBundleURLSchemes添加到LSApplicationQueriesSchemes .

    如果您尝试解决“canOpenURL:failed”警告,那些仅表示您的设备或模拟器上未安装Facebook应用程序,可以忽略 .

  • 0

    我认为错误状态10814与 cantOpenUrl 有关,当Facebook使用参数fbauth2:/调用URL时使用 . 喜欢建议here . 打印发生在此功能内部,因此您无法做任何事情

    您也可以在“设置”中更改此设置:

    目标>功能>启用钥匙串共享

    这并不重要,因为您正在模拟器中进行测试,并且当您在真实设备中进行测试时它将不会出现 .

  • 1

    mb for fb登录只能更好用FacebookLogin

    令牌:

    FBSDKAccessToken.current().tokenString
    

相关问题