如何使用 Xcode 4 从 Facebook 获取用户数据

Posted

技术标签:

【中文标题】如何使用 Xcode 4 从 Facebook 获取用户数据【英文标题】:How to get users data from Facebook using Xcode 4 【发布时间】:2012-08-23 11:23:35 【问题描述】:

我正在做一个应用程序,我想允许用户从 Facebook 注册,所以,我想从 Facebook 获取姓名、电子邮件和性别,但找不到获取数据的方法,如果有人可以帮忙的话。

这是我的代码示例:

// extract the id's for which we will request the profile
    NSArray *fbids = [NSArray arrayWithObjects:@"name",@"email",@"gender", nil];

    // create the connection object
    FBRequestConnection *newConnection = [[FBRequestConnection alloc] init];

    // for each fbid in the array, we create a request object to fetch
    // the profile, along with a handler to respond to the results of the request
    for (NSString *fbid in fbids) 

        // create a handler block to handle the results of the request for fbid's profile
        FBRequestHandler handler = 
        ^(FBRequestConnection *connection, id result, NSError *error) 

            // output the results of the request
            [self requestCompleted:connection forFbID:fbid result:result error:error];

        ;

        // create the request object, using the fbid as the graph path
        // as an alternative the request* static methods of the FBRequest class could
        // be used to fetch common requests, such as /me and /me/friends
        FBRequest *request = [[FBRequest alloc] initWithSession:FBSession.activeSession
                                                      graphpath:fbid];

        // add the request to the connection object, if more than one request is added
        // the connection object will compose the requests as a batch request; whether or
        // not the request is a batch or a singleton, the handler behavior is the same,
        // allowing the application to be dynamic in regards to whether a single or multiple
        // requests are occuring
        [newConnection addRequest:request completionHandler:handler];
    



// FBSample logic

// 报告任何结果。为我们提出的每个请求调用一次。 - (void)requestCompleted:(FBRequestConnection *)connection forFbID:fbID 结果:(id)结果 错误:(NSError *)错误

// not the completion we were looking for...
if (self.requestConnection &&
    connection != self.requestConnection) 
    return;


// clean this up, for posterity
self.requestConnection = nil;

NSString *nombre;
NSString *localizacion;
NSString *genero;
NSString *cumpleanyos;
NSString *email;

if (error) 
    // error contains details about why the request failed
    nombre = error.localizedDescription;
 else 
    // result is the json response from a successful request
    NSDictionary *dictionary = (NSDictionary *)result;
    // we pull the name property out, if there is one, and display it
    nombre = (NSString *)[dictionary objectForKey:@"name"];
    localizacion = (NSString *)[dictionary objectForKey:@"location"];
    genero = (NSString *)[dictionary objectForKey:@"gender"];
    cumpleanyos = (NSString *)[dictionary objectForKey:@"birthday"];
    email = (NSString *)[dictionary objectForKey:@"email"];

【问题讨论】:

【参考方案1】:

我认为问题出在:

NSArray *fbids = [NSArray arrayWithObjects:@"name",@"email",@"gender", nil];

您需要使用“FaceBook 用户 ID”尝试:

NSString *camposUsuario = @"4,6";
NSArray *fbids = [camposUsuario componentsSeparatedByString:@","];

在这种情况下,“4”和“6”是“FaceBook 用户 ID”。

如果您想请求特定用户的信息,您需要知道他的“FaceBook 用户 ID”。

【讨论】:

以上是关于如何使用 Xcode 4 从 Facebook 获取用户数据的主要内容,如果未能解决你的问题,请参考以下文章

Facebook sdk 3.0 与 iOS 5 和 Xcode 4.2

使用php创建应用程序ID后如何从facebook获取用户详细信息

Xcode 如何更新 Facebook sdk

Facebook 登录不会重定向到 Xcode 5 中自己的应用程序,但它在 Xcode 4 中工作

Facebook iOS SDK - 如何为 Xcode 4.3 构建静态库?

Xcode:将设备相册中的视频发布到 Facebook