【发布时间】:2011-07-14 09:20:17
【问题描述】:
一旦有人通过我的应用程序登录 facebook,我如何获取用户名?
我正在使用:- FBConnect API。
我的 Controller.m 文件-
#import "FacebookPOCViewController.h"
@implementation FacebookPOCViewController
@synthesize session = _session;
@synthesize logoutButton = _logoutButton;
@synthesize loginDialog = _loginDialog;
@synthesize facebookName = _facebookName;
- (void)viewDidLoad {
//PayTai Facebook App
static NSString* kApiKey = @"230600000000";
static NSString* kApiSecret = @"----------------------";
_session = [[FBSession sessionForApplication:kApiKey secret:kApiSecret delegate:self] retain];
// Load a previous session from disk if available. Note this will call session:didLogin if a valid session exists.
[_session resume];
[super viewDidLoad];
}
- (IBAction)loginTapped:(id)sender {
//_posting = YES;
// If we're not logged in, log in first...
if (![_session isConnected]) {
self.loginDialog = nil;
_loginDialog = [[FBLoginDialog alloc] init];
[_loginDialog show];
}
// If we have a session and a name, post to the wall!
else if (_facebookName != nil) {
//[self postToWall];
printf("Session");
}
// Otherwise, we don't have a name yet, just wait for that to come through.
}
- (IBAction)logoutButtonTapped:(id)sender {
[_session logout];
}
#pragma mark FBSessionDelegate methods
- (void)session:(FBSession*)session didLogin:(FBUID)uid {
[self getFacebookName];
}
- (void)session:(FBSession*)session willLogout:(FBUID)uid {
_logoutButton.hidden = YES;
_facebookName = nil;
}
pragma mark Get Facebook Name Helper
- (void)getFacebookName {
NSString* fql = [NSString stringWithFormat:@"select uid,name from user where uid == %lld", _session.uid];
//NSLog(@"%@",_session.uid);
NSDictionary* params = [NSDictionary dictionaryWithObject:fql forKey:@"query"];
[[FBRequest requestWithDelegate:self] call:@"facebook.fql.query" params:params];
}
#pragma mark FBRequestDelegate methods
- (void)request:(FBRequest*)request didLoad:(id)result {
if ([request.method isEqualToString:@"facebook.fql.query"]) {
NSArray* users = result;
NSDictionary* user = [users objectAtIndex:0];
NSString* name = [user objectForKey:@"name"];
self.facebookName = name;
_logoutButton.hidden = NO;
[_logoutButton setTitle:[NSString stringWithFormat:@"Logout as %@", name] forState:UIControlStateNormal];
//if (_posting) {
// [self postToWall];
// _posting = NO;
// }
}
}
【问题讨论】:
-
试试 NSLog(@"%@",facebookName);在 - (void)request:(FBRequest*)request didLoad:(id)result 方法....
-
^^ 这是打印配置文件的“名称”。它没有给我登录的“ID”。我想要用户名/ID?有什么线索吗?
-
[user objectForKey:@"uid"];是您个人资料的 uid
-
Bonny - 工作正常..谢谢...我可以获得用户名吗??
标签: iphone objective-c ios facebook