2016-04-23 49 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 
     } 
    } 
}