Getting username and profile picture from Facebook iOS 7

后端 未结 7 1103
悲&欢浪女
悲&欢浪女 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 04:59

    Actually using "http://graph.facebook.com//picture?type=small" to fetch the profile image of the user or even their friends is slow.

    A better and faster way of doing it to add a FBProfilePictureView object to your view and in it's profileID property, assign the user's Facebook id.

    For example: FBProfilePictureView *friendsPic;

    friendsPic.profileID = @"1379925668972042";

    0 讨论(0)
  • 2020-12-01 05:04
    if ([FBSDKAccessToken currentAccessToken]) {
        [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{ @"fields" : @"id,name,picture.width(100).height(100)"}]startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
            if (!error) {
                NSString *nameOfLoginUser = [result valueForKey:@"name"];
                NSString *imageStringOfLoginUser = [[[result valueForKey:@"picture"] valueForKey:@"data"] valueForKey:@"url"];
                NSURL *url = [[NSURL alloc] initWithURL: imageStringOfLoginUser];
                [self.imageView setImageWithURL:url placeholderImage: nil];
            }
        }];
    }
    
    0 讨论(0)
  • 2020-12-01 05:06

    This is the simplest way I've found to get the user's profile picture.

    [[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *FBuser, NSError *error) {
        if (error) {
          // Handle error
        }
    
        else {
          NSString *userName = [FBuser name];
          NSString *userImageURL = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large", [FBuser objectID]];
        }
      }];
    

    Other query parameters that can be used are:

    • type: small, normal, large, square
    • width: < value >
    • height: < value >
      • Use both width and height to get a cropped, aspect fill image
    0 讨论(0)
  • 2020-12-01 05:09

    Check out this lib: https://github.com/jonasman/JNSocialDownload

    you can even get twitter

    0 讨论(0)
  • 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<String, AnyObject>{
                    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)")
            }
        })
    
    0 讨论(0)
  • 2020-12-01 05:14

    You could also get the username and picture as follows:

    [FBSession openActiveSessionWithReadPermissions:@[@"basic_info"]
                                               allowLoginUI:YES
                                          completionHandler:
             ^(FBSession *session, FBSessionState state, NSError *error) {
    
                 if(!error && state == FBSessionStateOpen) {
                     { [FBRequestConnection startWithGraphPath:@"me" parameters:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"id,name,first_name,last_name,username,email,picture",@"fields",nil] HTTPMethod:@"GET" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                                 NSDictionary *userData = (NSDictionary *)result;
                                 NSLog(@"%@",[userData description]);
                             }];
                     }
                 }
             }];
    
    Output:
    picture =     {
            data =         {
                "is_silhouette" = 0;
                url = "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-frc1/t5.0-1/xxxxxxxxx.jpg";
            };
        };
        username = xxxxxxxxx;
    

    You could just leave the parameter to picture & username and exclude the others based on you requirement. HTH.

    0 讨论(0)
提交回复
热议问题