【问题标题】:How to get users data from Facebook using Xcode 4如何使用 Xcode 4 从 Facebook 获取用户数据
【发布时间】:2023-03-29 14:41:02
【问题描述】:

我正在做一个应用程序,我想允许用户从 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"];
}

}

【问题讨论】:

    标签: objective-c facebook xcode4


    【解决方案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”。

    【讨论】:

      猜你喜欢
      • 2019-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多