2015-05-20 141 views
2

我使用Facebook的IOS SDK 4.1圖請求返回登錄後無

,我使用此代碼來獲得用戶點擊按鈕後,用戶的信息和登錄

func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) { 


     let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil) 
     graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in 

      if ((error) != nil) 
      { 
       println("Error: \(error)") 
      } 
      else 
      { 
       var fbuseremail = result.valueForKey("email") as! NSString 
       var fbfirstname = result.valueForKey("first_name") as! NSString 
       var fblastname = result.valueForKey("last_name") as! NSString 
       var fbusername = result.valueForKey("name") as! NSString 

      //....more code 

但result.valueForKey(「電子郵件「)和其他人返回零

任何想法如何解決這個問題?在Facebook的SDK 4.x的

回答

3

獲取用戶信息迅速

@IBAction func btnFBLoginPressed(sender: AnyObject) { 
    var fbLoginManager : FBSDKLoginManager = FBSDKLoginManager() 
    fbLoginManager .logInWithReadPermissions(["email"], handler: { (result, error) -> Void in 
     if (error == nil){ 
      var fbloginresult : FBSDKLoginManagerLoginResult = result 
      if(fbloginresult.grantedPermissions.containsObject("email")) 
      { 
       self.getFBUserData() 
       fbLoginManager.logOut() 
      } 
     } 
    }) 
} 

func getFBUserData(){ 
    if((FBSDKAccessToken.currentAccessToken()) != nil){ 
     FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).startWithCompletionHandler({ (connection, result, error) -> Void in 
      if (error == nil){ 
       println(result) 
      } 
     }) 
    } 
} 

輸出:

{ 
    email = "[email protected]"; 
    "first_name" = Ashish; 
    id = 910855688971343; 
    "last_name" = Kakkad; 
    name = "Ashish Kakkad"; 
    picture =  { 
     data =   { 
      "is_silhouette" = 0; 
      url = "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xpf1/v/t1.0-1/p200x200/10394859_900936369963275_5557870055628103117_n.jpg?oh=fefbfca1272966fc78286c36741f9ac6&oe=55C89225&__gda__=1438608579_9133f15e55b594f6ac2306d61fa6b6b3"; 
     }; 
    }; 
} 
+0

我得到的 「設置沒有一個名爲 'ContainsObject' 成員」 在哪裏錯誤 –

+0

? @ RULE101我添加了自定義fb按鈕登錄代碼 –

+0

fbloginresult.grantedPermissions.containsObject(「email」)應該是fbloginresult.grantedPermissions.contains(「email」)我猜? –