1

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

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

getAuthValueForKey:方法没有返回任何东西给我。如何获取他们的 Facebook 用户名?

4

1 回答 1

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_;
}
于 2011-03-03T13:34:13.510 回答