如何在ios中获取Facebook用户信息

我试图开发一个简单的应用程序,它从Facebook检索数据,当用户连接到它。 我试过这个代码。

NSArray *permissions = [[NSArray alloc] initWithObjects:@"user_birthday",@"user_hometown",@"user_location",@"email",@"basic_info", nil]; [FBSession openActiveSessionWithReadPermissions:permissions allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) { }]; [FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) { NSLog(@"%@", [result objectForKey:@"gender"]); NSLog(@"%@", [result objectForKey:@"hometown"]); NSLog(@"%@", [result objectForKey:@"birthday"]); NSLog(@"%@", [result objectForKey:@"email"]); }]; 

但是当我运行这个代码时,它会给出一个错误“FBSDKLog:请求到端点'me'的错误:必须为调用这个端点指定一个开放的FBSession。

在此先感谢,非常感谢您的帮助。

错误是非常合适的,它试图说的是,一旦会话打开,应该调用请求连接方法。 现在你的

 [FBSession openActiveSessionWithReadPermissions:permissions allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) { }]; 

方法返回BOOL值true或false来指定你的会话是否打开(它试图同步打开)。 所以首先检查这个调用的结果,并把它放在获取信息的代码中。 例如。

  if (FBSession.activeSession.isOpen) { [FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) { NSLog(@"%@", [result objectForKey:@"gender"]); NSLog(@"%@", [result objectForKey:@"hometown"]); NSLog(@"%@", [result objectForKey:@"birthday"]); NSLog(@"%@", [result objectForKey:@"email"]); }]; } 

这应该删除你的错误,但你仍然可能得不到结果。你可能会也可能不会得到结果的第一次调用这个代码,但每当完成处理程序的代码将被调用,这个方法FBRequestConnection也将被调用,并在那个时候你会得到结果,因为它是一个asynchronous调用。

如果还是不行的话试试这个

  if (FBSession.activeSession.isOpen) { [[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) { if (error) { NSLog(@"error:%@",error); } else { // retrive user's details at here as shown below NSLog(@"FB user first name:%@",user.first_name); NSLog(@"FB user last name:%@",user.last_name); NSLog(@"FB user birthday:%@",user.birthday); } }]; 

`(void)fbAccountConfigureWithBlock:(void(^)(id,NSString *))block {_block_data = block;

 if(![SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) { dispatch_async(dispatch_get_main_queue(), ^{ [self showAlertMessage:@"" message:@"Please go to settings and add at least one facebook account."]; _block_data(nil,nil); }); return; } ACAccountStore *store = [[ACAccountStore alloc]init]; ACAccountType *accountType = [store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; [store requestAccessToAccountsWithType:accountType options:@{ACFacebookAppIdKey : FacebookAppId, ACFacebookAudienceKey : ACFacebookAudienceFriends, ACFacebookPermissionsKey : @[@"email"]} completion:^(BOOL granted, NSError *error) { if(granted){ NSArray *array = [store accountsWithAccountType:accountType]; if(!array.count){ dispatch_sync(dispatch_get_main_queue(), ^{ [self showAlertMessage:@"" message:@"Please go to settings and add at least one facebook account."]; _block_data(nil,nil); }); } else{ ACAccount *account = array[0]; SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodGET URL:[NSURL URLWithString:@"https://graph.facebook.com/me"] parameters: @{@"fields":@"id,first_name,last_name,name,email,picture.height(180).width(180)"}]; [request setAccount:account]; [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { if(!error){ NSDictionary *userData = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil]; NSLog(@"Facebook user data ----> %@",userData); dispatch_async(dispatch_get_main_queue(), ^{ if(userData[@"error"] != nil) [self attemptRenewCredentials:store account:account]; else _block_data(userData,nil); }); } else{ dispatch_async(dispatch_get_main_queue(), ^{ [self showAlertMessage:@"" message:error.localizedDescription]; _block_data(nil,nil); }); } }]; } } else { dispatch_async(dispatch_get_main_queue(), ^{ [self showAlertMessage:@"" message:@"We need permission to access your facebook account in order make registration."]; _block_data(nil,nil); }); } }]; 

}`