1

ShareKitを使用していて、ユーザーがFacebookにログインしていることを確認しました。Facebookのユーザー名を取得したい。これが私が試したことです:

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

メソッドはgetAuthValueForKey:私に何も返しません。Facebookのユーザー名を取得するにはどうすればよいですか?

4

1 に答える 1

4

FacebookGraphAPIを使用してユーザー名を取得できます。以下のメソッドを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 に答える