开发者

In ShareKit, how can I get the user's Facebook user name?

I'm using ShareKit and 开发者_JS百科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?


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_;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜