2015-07-13 16 views
11

蔭(使用SWIFT)的iOS facebookSDK使用最後FBSDK獲得用戶的全部細節

//MARK: sign in with facebook 
func signInWithFacebook() 
{ 
    if (FBSDKAccessToken.currentAccessToken() != nil) 
    { 
     // User is already logged in, do work such as go to next view controller. 
     println("already logged in ") 
     self.returnUserData() 

     return 
    } 
    var faceBookLoginManger = FBSDKLoginManager() 
    faceBookLoginManger.logInWithReadPermissions(["public_profile", "email", "user_friends"], handler: { (result, error)-> Void in 
     //result is FBSDKLoginManagerLoginResult 
     if (error != nil) 
     { 
      println("error is \(error)") 
     } 
     if (result.isCancelled) 
     { 
      //handle cancelations 
     } 
     if result.grantedPermissions.contains("email") 
     { 
      self.returnUserData() 
     } 
    }) 
} 

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

     if ((error) != nil) 
     { 
      // Process error 
      println("Error: \(error)") 
     } 
     else 
     { 
      println("the access token is \(FBSDKAccessToken.currentAccessToken().tokenString)") 

      var accessToken = FBSDKAccessToken.currentAccessToken().tokenString 

      var userID = result.valueForKey("id") as! NSString 
      var facebookProfileUrl = "http://graph.facebook.com/\(userID)/picture?type=large" 



      println("fetched user: \(result)") 


} 

當我打印的獲取用戶我只得到了id和名字! , 但我請求了電子郵件和朋友以及個人資料的許可, 有什麼問題?

順便說一句:我將這個項目從我的MacBook移動到另一個macbook(因爲我格式化了我的)它工作得很好,當它在我創建該項目的macbook上,但移動項目後(使用bitbucket克隆)我得到了這個結果。

+1

refer thi s鏈接,我希望能解決你的問題http://stackoverflow.com/a/30634444/4536708 @ user3703910 –

+0

**問題解決重複發佈:** http://stackoverflow.com/a/31503463/3382676 – HeTzi

回答

33

根據新的Facebook SDK,你必須與FBSDKGraphRequest

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

之證件鏈接傳遞參數:https://developers.facebook.com/docs/facebook-login/permissions/v2.4

用戶對象的引用:https://developers.facebook.com/docs/graph-api/reference/user

隨着公衆形象,你可以獲取性別:

public_profile (Default) 

Provides access to a subset of items that are part of a person's public profile. A person's public profile refers to the following properties on the user object by default: 

id 
name 
first_name 
last_name 
age_range 
link 
gender 
locale 
timezone 
updated_time 
verified 
+0

你能直接我到他們的文檔? ,如何獲得性別? – user3703910

+0

@ user3703910更新了答案(Y) –

+0

如何從結果中獲取值?我需要獲得電子郵件ID,名字和姓氏等如何做到這一點?我試着用result.valueForKey(「email」)。它不幫助我。 – Noorul

4

我想此代碼應幫助您獲得所需的細節

斯威夫特2.x的

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

     if ((error) != nil) 
     { 
      // Process error 
      print("Error: \(error)") 
     } 
     else 
     { 
      print("fetched user: \(result)") 
      let userName : NSString = result.valueForKey("name") as! NSString 
      print("User Name is: \(userName)") 
      let userID : NSString = result.valueForKey("id") as! NSString 
      print("User Email is: \(userID)") 



     } 
    }) 
+0

獲取「Any?」類型的價值沒有成員的'valueForKey'錯誤信息 –

+0

上面的答案是Swift 2.x,現在在swift 3.x中試試。 (我也會爲swift 3更新上面的答案) let data:[String:AnyObject] = result as! [字符串:AnyObject] 打印(數據[ 「如first_name」]!) 打印(數據[ 「ID」]!) –

1

斯威夫特4

斯威夫特4的例子也說明了如何正確解析從結果中取出個別字段:

func fetchFacebookFields() { 
    //do login with permissions for email and public profile 
    FBSDKLoginManager().logIn(withReadPermissions: ["email","public_profile"], from: nil) { 
     (result, error) -> Void in 
     //if we have an error display it and abort 
     if let error = error { 
      log.error(error.localizedDescription) 
      return 
     } 
     //make sure we have a result, otherwise abort 
     guard let result = result else { return } 
     //if cancelled nothing todo 
     if result.isCancelled { return } 
     else { 
      //login successfull, now request the fields we like to have in this case first name and last name 
      FBSDKGraphRequest(graphPath: "me", parameters: ["fields" : "first_name, last_name"]).start() { 
       (connection, result, error) in 
       //if we have an error display it and abort 
       if let error = error { 
        log.error(error.localizedDescription) 
        return 
       } 
       //parse the fields out of the result 
       if 
        let fields = result as? [String:Any], 
        let firstName = fields["first_name"] as? String, 
        let lastName = fields["last_name"] as? String 
       { 
        log.debug("firstName -> \(firstName)") 
        log.debug("lastName -> \(lastName)") 
       } 
      } 
     } 
    } 
} 
+0

好榜樣雨燕4,2日錯字到最後一行雖然firsName - >的firstName – shokaveli

+0

THX爲反饋@shokaveli糾正它:) – HixField

相關問題