Getting username and profile picture from Facebook iOS 7

后端 未结 7 1126
悲&欢浪女
悲&欢浪女 2020-12-01 04:45

I have read a lot of tutorials about getting information from Facebook, but I have failed so far. I just want to get username and profile picture from Facebook.



        
7条回答
  •  醉梦人生
    2020-12-01 05:11

    This is the code for Facebook SDK 4 and Swift:

    if FBSDKAccessToken.currentAccessToken() != nil {
        FBSDKGraphRequest(graphPath: "me", parameters: nil).startWithCompletionHandler({ (connection, result, error) -> Void in
            println("This logged in user: \(result)")
            if error == nil{
                if let dict = result as? Dictionary{
                    println("This is dictionary of user infor getting from facebook:")
                    println(dict)
                }
            }
        })
    }
    

    UPDATE TO ANSWER QUESTIONS:

    To download public profile image, you get the facebook ID from the dictionary:

    let facebookID:NSString = dict["id"] as AnyObject? as NSString
    

    And then call a request to graph API for profile image using the facebook ID:

    let pictureURL = "https://graph.facebook.com/\(fbUserId)/picture?type=large&return_ssl_resources=1"
    

    Sample code:

        let pictureURL = "https://graph.facebook.com/\(fbUserId)/picture?type=large&return_ssl_resources=1"
        //
        var URLRequest = NSURL(string: pictureURL)
        var URLRequestNeeded = NSURLRequest(URL: URLRequest!)
        println(pictureURL)
    
    
    
        NSURLConnection.sendAsynchronousRequest(URLRequestNeeded, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!, error: NSError!) -> Void in
            if error == nil {
                //data is the data of profile image you need. Just create UIImage from it
    
            }
            else {
                println("Error: \(error)")
            }
        })
    

提交回复
热议问题