我想使用 FacebookSDK 实现功能。
作为示例应用程序,您可以检查 url:
https://developers.facebook.com/docs/facebook-login/handling-declined-permissions#reprompt
我已经编写了这段代码,但它并没有像我预期的那样工作。
//Callback function for default FBLogin Button
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!)
{
print("User Logged In")
if (error != nil)
{
// Process error
print("Processing Error : \(error)")
FBSDKLoginManager().logOut()
self.dismissViewControllerAnimated(true, completion: nil)
}
else if result.isCancelled
{
// Handle cancellations
print("user is cancelled the login FB")
FBSDKLoginManager().logOut()
self.dismissViewControllerAnimated(true, completion: nil)
}
else
{
print("result : \(result)")
// If you ask for multiple permissions at once, you
// should check if specific permissions missing
if result.declinedPermissions.contains("email")
{
print("email is declined")
// Do work
loginManager = FBSDKLoginManager()
loginManager!.logInWithReadPermissions(["email"], fromViewController: self, handler:{ [unowned self](result, error) -> Void in
if error == nil
{
self.fetchUserData()
}
})
}
else
{
var readPermissions : FBSDKLoginManagerLoginResult = result
Constants.isUserLoggedIn = true
fetchUserData()
}
}
}
最佳答案
我在提供的片段中遇到了一些问题,我将逐步解决这些问题。修改了底部的代码。
编译错误
当我尝试按照给定的方式运行您的代码时,出现编译错误
loginManager = FBSDKLoginManager()
loginManager!.logInWithReadPermissions(["email"], fromViewController: self, handler:{ [unowned self](result, error) -> Void in
use of unresolved identifier 'loginManager'
从外观上看,您已经在 View Controller 上保存了一个可选的 FBSDKLoginManager,但这不是必需的,并且会打乱您重新提示用户输入电子邮件的尝试。
他们不会再给您访问电子邮件的机会,而只会看到“您已经授权 [此处的应用程序名称]”对话框。
(不幸的是,“重新请求”是挑剔和隐含的......我从这篇文章中学到了我所知道的一切,这并不多-How to “rerequest” email permission using Facebook iOS SDK 4.x?)
时间
另一个主要问题似乎与您调用重新请求权限的时间有关。当我运行您的代码并取消检查电子邮件访问权限时,我看到了一个空白的 Facebook 弹出窗口。
但是,当我在示例应用程序中将重新提示包装在一个对话框中并解释我需要电子邮件的目的时,我看到了我期待的重新提示。
其他
- 为您的重新提示尝试添加了错误处理(否则您会强制解包零错误)
- 删除了不必要的调用
self.dismissViewControllerAnimated(true, completion: nil)
修改后的代码
//Callback function for default FBLogin Button
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!)
{
print("User Logged In")
if (error != nil)
{
// Process error
print("Processing Error : \(error)")
FBSDKLoginManager().logOut()
}
else if result.isCancelled
{
// Handle cancellations
print("user is cancelled the login FB")
FBSDKLoginManager().logOut()
}
else //permissions were granted, but still need to check which ones
{
if result.declinedPermissions.contains("email")
{
let alert = UIAlertController(title: "Alert", message: "We need your email address to proceed", preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: { action in
// Handle cancellations
print("user is cancelled the login FB")
FBSDKLoginManager().logOut()
})
let reRequestAction = UIAlertAction(title: "Grant Access", style: UIAlertActionStyle.Default, handler: { action in
let fbsdklm = FBSDKLoginManager()
fbsdklm.logInWithReadPermissions(["email"], fromViewController: self) { (result, error) -> Void in
if (error != nil)
{
// Process error
print("Processing Error : \(error)")
FBSDKLoginManager().logOut()
}
else if result.isCancelled {
// Handle cancellations
print("user is cancelled the login FB")
FBSDKLoginManager().logOut()
}
else {
print("Got Email Permissions!")
//proceed
}
}
})
alert.addAction(cancelAction)
alert.addAction(reRequestAction)
self.presentViewController(alert, animated: true, completion: nil)
}
else
{
print("Got Email Permissions!")
//proceed
}
}
}
关于ios - FacebookSDK如何重新提示用户同意?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36808659/