2016-06-02 26 views
5

Buon pomeriggio! Devo ottenere i dati dell'utente da Facebook. Ricevo correttamente dati come nome utente, ID, foto. Ma quando si tenta di interrogare come stato civile, ottengo una risposta a un dizionario vuoto. Ho provato a fare richieste diverse, ma ottengo sempre un risultato simile. Leggo anche questi temi official Facebook site, this, this come posso risolvere questo?come ottenere i dati utente da Facebook SDK su iOS

Il mio codice di esempio

static func getUserRelationship() { 
    if (FBSDKAccessToken.currentAccessToken() != nil) { 
     guard let currentUser = getCurrentFacebookUser() else { return } 
     FBSDKGraphRequest(graphPath: "/\(currentUser.id)/family", parameters: nil, HTTPMethod: "GET").startWithCompletionHandler({ (requestConnection, result, error) in 
      if error == nil { 
       let dictionary = result as! [String : AnyObject] 
       let array = dictionary["data"] 
       print("facebook", result, dictionary, array?.count) 
      } else { 
       print(error.localizedDescription) 
      } 
     }) 
    } else { 
     getDataLogin({ 
      getUserBirthday() 
      }, fails: { (error) in 

     }) 
    } 
} 

vedo il risultato nella console

facebook { 
    data =  (
    ); 
} 
+1

Avete chiesto le autorizzazioni appropriate? user_relationships https://developers.facebook.com/docs/facebook-login/permissions#reference-user_relationships – TommyBs

risposta

13

tuo GraphRequest non era corretta. Se vuoi prendere i dati dell'utente, graphPathe dovrebbe essere "me" e dovresti richiedere il parametro per ottenere lo stato della relazione e altre informazioni. E inoltre è necessario richiedere profilo pubblico in LogInWithReadPermissons, così in lettura permisson: -

let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager() 
    fbLoginManager.loginBehavior = FBSDKLoginBehavior.Web 
    fbLoginManager.logInWithReadPermissions(["public_profile","email"], fromViewController: self) { (result, error) -> Void in 
     if error != nil { 
      print(error.localizedDescription) 
      self.dismissViewControllerAnimated(true, completion: nil) 
     } else if result.isCancelled { 
      print("Cancelled") 
      self.dismissViewControllerAnimated(true, completion: nil) 
     } else { 

     } 
    } 

e quando il recupero delle informazioni: -

FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, relationship_status"]).startWithCompletionHandler({ (connection, result, error) -> Void in 
      if (error == nil){ 
       let fbDetails = result as! NSDictionary 
       print(fbDetails) 
      } 
     }) 

Tra l'altro, se si desidera lo stato civile si dovrebbe usare graphPath come "me" non "/ {user-id}/famiglia". Puoi usarlo per ottenere le relazioni familiari della persona.

+1

Grazie mille. Mi hai aiutato! – Alexander

2

get update informazioni utente facebook con il rapido 3

FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email"]).start(completionHandler: { (connection, result, error) -> Void in 
     if (error == nil){ 
      let fbDetails = result as! NSDictionary 
      print(fbDetails) 
     }else{ 
      print(error?.localizedDescription ?? "Not found") 
     } 
    }) 
Problemi correlati