gpt4 book ai didi

iphone - 在iPhone App中获取Facebook用户名

转载 作者:行者123 更新时间:2023-12-01 17:27:59 31 4
gpt4 key购买 nike

一旦有人通过我的应用程序登录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;
// }
}
}

最佳答案

看看这个。您可以使用GRAPH API获取用户的几乎所有信息。

http://developers.facebook.com/docs/reference/api/user/


  /**
* Request the facebook name for the user
* Response will be obtained on delegate
*/
- (void) getFacebookName {

[facebook requestWithGraphPath:@"me?fields=id,name" andDelegate:self];
}

#pragma mark - FBRequestDelegate methods

- (void)request:(FBRequest *)request didLoad:(id)result {

NSLog(@"Result: %@", result);
NSDictionary *userInfo = (NSDictionary *)result;
userName = [userInfo objectForKey:@"name"];
fb_id = [userInfo objectForKey:@"id"];
}

关于iphone - 在iPhone App中获取Facebook用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6690999/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com