ios - 在Xcode Swift Facebook登录SDK中无法获取访问 token

标签 ios xcode facebook swift3

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

-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登录对话框,但此后我仅看到一个对话框,说我已经使用“确定”按钮授权了我的应用程序。
let accessToken = AccessToken.current

在viewDidLoad()中始终返回nil。我需要获取一个accessToken,以便可以将其发送到API并测试响应。

最佳答案

mb for fb login仅更好地使用FacebookLogin

这里的 token :

FBSDKAccessToken.current().tokenString

关于ios - 在Xcode Swift Facebook登录SDK中无法获取访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44333099/

相关文章:

ios - titleLabel 无法在 UIButton 中正确适应高度

iphone - 自动发送消息,无需用户交互

iphone - 如何使 UITableView 上的 SwipeGestureRecognizer 响应更快?

更新后 Xcode 找不到模块

ios - Facebook iOS SDK 4.x : changing the user to log in

php - 如何使用适用于 Facebook 的 PHP SDK 显示好友选择器对话框?

mysql - iOS 5 : How can I preserve newline while retrieving values from mysql via PHP in UITextView and UIWebView

ios - Apple LLVM 编译器 4.2 错误

iOS:我可以在后台读取 SQLite 数据库吗?

facebook - 使用开放图谱协议(protocol)将站点与 Facebook 页面链接