私はアプリをやっていて、ユーザーが 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
// 結果を報告します。リクエストごとに 1 回呼び出されます。- (void)requestCompleted:(FBRequestConnection *)connection forFbID:fbID result:(id)result error:(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"];
}
}