我正在做一个应用程序,我想允许用户从 Fb 注册,所以,我想从 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)result 错误:(NSError *)error {
// 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"];
}
}