我正在尝试在我的应用中测试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/