1

I'm using ShareKit and have verified that the user is logged in to Facebook. I want to get their Facebook user name. Here's what I tried:

SHKFacebook *facebook = [[SHKFacebook alloc] init];
[facebook getAuthValueForKey:@"username"];

The getAuthValueForKey: method isn't returning anything to me. How can I get their Facebook user name?

Josh Brown
  • 52,385
  • 10
  • 54
  • 80
  • This questions was asked: http://stackoverflow.com/questions/4772378/getting-user-id-of-facebook-and-twitter-in-sharekit with no answer still *unfortunately* – Andrew Jackman Jan 25 '11 at 08:10

1 Answers1

4

You can use Facebook Graph API for get username. Add method bellow to FBSession.m class of FBConnect package.

#import "JSON.h"

...........

static NSString *const kGraphAPIURL = @"https://graph.facebook.com/";
static NSString *const kFBGraphAPIUserName = @"name";

...........

// Get user name via Facebook GraphAPI.
- (NSString *)getUserNameByUID:(FBUID)aUID {

    NSString *userName_ = nil;

    NSURL *serviceUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@%qi", kGraphAPIURL, aUID]];

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    NSError *error = nil;
    NSString *jsonString = [NSString stringWithContentsOfURL:serviceUrl encoding:NSUTF8StringEncoding error:&error];

    if (error != nil) {
       NSLog(@"######### error: %@", error);
    }

    if (jsonString) {

        // Parse the JSON into an Object and 
        // serialize its object to NSDictionary
        NSDictionary *resultDic = [jsonString JSONValue];

        if (resultDic) {
            userName_ = [resultDic valueForKey:kFBGraphAPIUserName];
        }
    }

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    return userName_;
}
berec
  • 775
  • 10
  • 19
  • Thanks for your response. I'm hoping to be able to use the ShareKit API to get their username, but maybe that's not possible... – Josh Brown Apr 29 '11 at 02:20
  • here what is the kGraphAPIURL,kFBGraphAPIUserName and shows the warning NSDictionary *resultDic = [jsonString JSONValue]; NSString may not respond to -JSONValue – Steve Gear May 19 '11 at 10:27
  • static NSString *const kGraphAPIURL = @"https://graph.facebook.com/"; static NSString *const kFBGraphAPIUserName = @"name"; and don't forget add #import "JSON.h" at the top of FBSession.m file – berec May 26 '11 at 08:58