2015-07-13 10 views
11

Iam मिलआईओएस 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)") 


} 

जब मैं मैं सिर्फ आईडी और नाम प्राप्त किए गए उपयोगकर्ता प्रिंट! , लेकिन मैंने ईमेल और दोस्तों और प्रोफ़ाइल के लिए अनुमति का अनुरोध किया, क्या गलत है ???

बीटीडब्ल्यू: मैंने इस परियोजना को मेरी मैकबुक से दूसरी मैकबुक में ले जाया है (क्योंकि मैंने मेरा स्वरूपित किया है) यह मैकबुक पर बहुत अच्छा काम करता था, जिस पर मैंने प्रोजेक्ट बनाया था, लेकिन प्रोजेक्ट को ले जाने के बाद (बिटबकेट क्लोन का उपयोग करके) मुझे यह परिणाम मिला।

+1

देखें thi लिंक मैं आशा आपकी समस्या http://stackoverflow.com/a/30634444/4536708 @ user3703910 –

+0

** अंक डुप्लिकेट पोस्ट में हल का समाधान: ** http://stackoverflow.com/a/31503463/3382676 – HeTzi

उत्तर

33

प्रति नई फेसबुक एसडीके के रूप में, आप के साथ 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

अद्यतन से मूल्य प्राप्त करने के लिए अद्यतन किया है? मुझे ईमेल आईडी, पहला नाम और अंतिम नाम इत्यादि प्राप्त करने की आवश्यकता है। यह कैसे करें? मैंने परिणाम के साथ प्रयास किया .valueForKey ("ईमेल")। यह मेरी मदद नहीं करता है। – 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

प्रकार का मूल्य 'कोई भी?' कोई सदस्य नहीं है 'valueForKey' त्रुटि संदेश –

+0

उपरोक्त उत्तर स्विफ्ट 2.x के लिए है, अब स्विफ्ट 3.x में इसे इस तरह से आजमाएं। देना डेटा (मैं तेजी से 3 के लिए भी ऊपर जवाब अपडेट करेगा): [स्ट्रिंग: AnyObject] = के रूप में परिणाम! [स्ट्रिंग: AnyObject] प्रिंट (डेटा [ "first_name"]!) प्रिंट (डेटा [ "आईडी"]!) स्विफ्ट 4 के लिए –

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

अच्छा उदाहरण, अंतिम पंक्ति हालांकि firsName को 2 पर टाइपो -> firstName – shokaveli

+0

के लिए Thx फीडबैक @ शोकवेली ने इसे सही किया :) – HixField

संबंधित मुद्दे