在iOS中从Facebook获取用户详细信息 [英] Fetching user details from Facebook in iOS

查看:162
本文介绍了在iOS中从Facebook获取用户详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iPhone开发的新手。我正在做一个用户通过Facebook登录2的应用程序,一旦他提交了他的凭据并点击登录按钮,我必须从Facebook获取诸如名字,电子邮件和性别的详细信息,并且在获取用户之后必须被定向到应用程序的注册页面填写了详细信息,我需要这个应用程序兼容iPhone 4,4s和5。

I'm new to iPhone development. I'm doing an App where the user has 2 login through Facebook, once he submits his credentials and clicks on Sign In button, I have to fetch details like First name, email and gender from Facebook and after fetching the user has to be directed to the Registration page of the app with the details filled and i need this app to be compatible for iPhone 4,4s and 5.

我尝试使用Facebook Graph这样做API但无法得到它,所以任何人都可以帮助我。

I tried doing this using the Facebook Graph API but couldn't get it, so anyone can help me out.

先谢谢。

推荐答案

您可以使用以下代码执行此操作:

You can do this by using following code :

[FBSession openActiveSessionWithReadPermissions:@[@"email",@"user_location",@"user_birthday",@"user_hometown"]
                                   allowLoginUI:YES
                              completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {

                                  switch (state) {
                                      case FBSessionStateOpen:
                                          [[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);
                                                  NSLog(@"FB user location:%@",user.location);
                                                  NSLog(@"FB user username:%@",user.username);
                                                  NSLog(@"FB user gender:%@",[user objectForKey:@"gender"]);
                                                  NSLog(@"email id:%@",[user objectForKey:@"email"]);
                                                  NSLog(@"location:%@", [NSString stringWithFormat:@"Location: %@\n\n",
                                                                         user.location[@"name"]]);

                                              }
                                          }];
                                          break;

                                      case FBSessionStateClosed:
                                      case FBSessionStateClosedLoginFailed:
                                          [FBSession.activeSession closeAndClearTokenInformation];
                                          break;

                                      default:
                                          break;
                                  }

                              } ];

并且不要忘记导入 FacebookSDK / FacebookSDK.h 在您的代码中。

and don't forgot to import FacebookSDK/FacebookSDK.h in your code.

编辑:Facebook SDK v4更新(2015年4月23日)

现在,Faceboook已发布了主要更改的新SDK。不推荐使用哪个FBSession类。因此建议所有用户迁移到新的sdk和API。

Now, Faceboook have released new SDK with major changes. In which FBSession class is deprecated. So all users are suggested to migrate to new sdk and APIs.

我已经提到过,我们如何通过Facebook SDK v4获取用户详细信息:

Below I have mentioned, how we can get user details via Facebook SDK v4 :

if ([FBSDKAccessToken currentAccessToken]) {
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
  if (!error) {
     NSLog(@"fetched user:%@", result);
  }
 }];
}

但在获取用户详细信息之前,我们必须在我们的代码中集成新的Facebook登录信息如此处的文档中所述。

But before fetching user details, we have to integrate new Facebook login in our code as described in Documentation here.

此处是SDK v4的更改日志。我建议通过它进行更新。

Here is the Changelog for SDK v4. I suggest going through it for being updated.

这篇关于在iOS中从Facebook获取用户详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆