在ShareKit中,如何获取用户的Facebook用户名?

时间:2011-01-25 08:05:40

标签: iphone facebook sharekit

我正在使用ShareKit并已验证用户已登录Facebook。我想获得他们的Facebook用户名。这是我试过的:

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

getAuthValueForKey:方法没有向我返回任何内容。我怎样才能获得他们的Facebook用户名?

1 个答案:

答案 0 :(得分:4)

您可以使用Facebook Graph API获取用户名。将方法添加到FBConnect包的FBSession.m类中。

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