2011-01-25 79 views
1

我正在使用ShareKit并验证用户已登录到Facebook。我想获得他们的Facebook用户名。以下是我试过的:在ShareKit中,我如何获取用户的Facebook用户名?

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

getAuthValueForKey:方法没有返回任何东西给我。我怎样才能得到他们的Facebook用户名?

+0

这个问题,有人问:HTTP:/ /stackoverflow.com/questions/4772378/getting-user-id-of-facebook-and-twitter-in-sharekit没有答案仍然*不幸* – 2011-01-25 08:10:17

回答

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

感谢您的回应。我希望能够使用ShareKit API来获取他们的用户名,但也许这是不可能的... – 2011-04-29 02:20:53