簡體   English   中英

在 iPhone 應用程序中獲取 Facebook 用戶名

[英]Getting Facebook Username in iPhone App

一旦有人通過我的應用程序登錄到 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"];
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM