2016-04-23 51 views
1

我想使用FacebookSDK实现功能。如何使用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() 
     } 
    } 
} 

回答

1

我遇到了一些问题,提供的代码片段,我会经历。修改底部的代码。

编译错误

当我尝试运行代码给出,我得到一个编译错误

loginManager = FBSDKLoginManager() 
loginManager!.logInWithReadPermissions(["email"], fromViewController: self, handler:{ [unowned self](result, error) -> Void in 

使用未解决的标识符 'loginManager'

的自它的外观,你已经在视图控制器上保存了一个可选的FBSDKLoginManager,但这不是必需的,而且wi会搞砸你试图重新提示用户的电子邮件。

而不是获得第二次机会让你访问电子邮件,他们只会看到“你已经授权[应用程序名称在这里]”对话框。

(该“再请求”是挑剔的和隐含的不幸......我学会了所有我知道,这不是很多,从这个后How to “rerequest” email permission using Facebook iOS SDK 4.x?

定时

的另一个主要问题似乎只是关于您的电话重新请求权限的时间。当我运行你的代码,并且未经检查的电子邮件访问时,我看到一个空白的Facebook Popup。

但是,当在示例应用程序中,我在对话框中将重新提示包装为解释我需要的电子邮件地址时,我看到了我期待的重新提示。

其他

  • 添加错误处理您重新提示尝试(否则你打零差错力展开)
  • 删除不必要的来电

    self.dismissViewControllerAnimated(真,完成:无)

修改过的代码

//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 
     } 
    } 
}