我想使用FacebookSDK实现功能。
作为示例应用程序,您可以查看网址:
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()
}
}
}
答案 0 :(得分:2)
我在提供的代码片段中遇到了一些问题,我将逐步介绍。修改后的代码。
编译错误
当我尝试按给定的方式运行代码时,出现编译错误
loginManager = FBSDKLoginManager()
loginManager!.logInWithReadPermissions(["email"], fromViewController: self, handler:{ [unowned self](result, error) -> Void in
使用未解析的标识符' loginManager'
从它的外观来看,你已经在你的视图控制器上保存了一个可选的FBSDKLoginManager,但这并不是必需的,并且会使你试图重新篡改用户的电子邮件。
他们只会看到"您已经授权[此处的应用名称]"而不是第二次给您访问电子邮件的机会。对话。
(" rerequest"遗憾的是,这是一种挑剔和隐含的......我从这个帖子How to “rerequest” email permission using Facebook iOS SDK 4.x?中学到了所有我知道的东西,但并不多。
<强>时序强>
另一个主要问题似乎是关于重新请求权限的调用时间。当我运行您的代码和未经检查的电子邮件访问时,我看到了一个空白的Facebook Popup。
然而,当我在示例应用程序中将重新提示包装在一个对话框中并解释我需要的电子邮件时,我看到了我期待的重新提示。
其他强>
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
}
}
}