我是 iPhone 开发的新手。我正在做一个应用程序,其中用户通过 Facebook 进行了 2 次登录,一旦他提交了他的凭据并点击了登录按钮,我必须从 Facebook 获取名字、电子邮件和性别等详细信息,并且在获取用户之后必须被引导到应用程序的注册页面,填写详细信息,我需要此应用程序与 iPhone 4,4s 和 5 兼容。
我尝试使用 Facebook Graph API 执行此操作,但无法获得它,因此任何人都可以帮助我。
提前致谢。
我是 iPhone 开发的新手。我正在做一个应用程序,其中用户通过 Facebook 进行了 2 次登录,一旦他提交了他的凭据并点击了登录按钮,我必须从 Facebook 获取名字、电子邮件和性别等详细信息,并且在获取用户之后必须被引导到应用程序的注册页面,填写详细信息,我需要此应用程序与 iPhone 4,4s 和 5 兼容。
我尝试使用 Facebook Graph API 执行此操作,但无法获得它,因此任何人都可以帮助我。
提前致谢。
您可以使用以下代码执行此操作:
[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
在您的代码中导入。
编辑:Facebook SDK v4 更新(2015 年 4 月 23 日)
现在,Facebook 发布了具有重大变化的新 SDK。其中不推荐使用 FBSession 类。因此建议所有用户迁移到新的 sdk 和 API。
下面我已经提到,我们如何通过 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 登录信息,如这里的文档中所述。
这是 SDK v4 的变更日志。我建议通过它进行更新。
添加 info.plist 文件:
- (IBAction)btn_fb:(id)sender
{
if (!FBSession.activeSession.isOpen)
{
NSArray *_fbPermissions = @[@"email",@"publish_actions",@"public_profile",@"user_hometown",@"user_birthday",@"user_about_me",@"user_friends",@"user_photos",];
[FBSession openActiveSessionWithReadPermissions:_fbPermissions allowLoginUI:YES completionHandler:^(FBSession *session,FBSessionState state, NSError *error)
{
if (error)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
else if(session.isOpen)
{
[self btn_fb:sender];
}
}];
return;
}
[FBRequestConnection startWithGraphPath:@"me" parameters:[NSDictionary dictionaryWithObject:@"cover,picture.type(large),id,name,first_name,last_name,gender,birthday,email,location,hometown,bio,photos" forKey:@"fields"] HTTPMethod:@"GET" completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
{
if (!error)
{
if ([result isKindOfClass:[NSDictionary class]])
{
NSLog(@"%@",result);
}
}
}
}];
}
FBRequest *request = [FBRequest requestForMe];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// handle successful response
} else if ([error.userInfo[FBErrorParsedJSONResponseKey][@"body"][@"error"][@"type"] isEqualToString:@"OAuthException"]) { // Since the request failed, we can check if it was due to an invalid session
NSLog(@"The facebook session was invalidated");
[self logoutButtonTouchHandler:nil];
} else {
NSLog(@"Some other error: %@", error);
}
}];
请参考以下链接,以便您有所了解。
https://www.parse.com/tutorials/integrating-facebook-in-ios
在 iOS 5 中获取访问令牌后获取 Facebook 用户个人资料数据
通过 iOS 的 Facebook 实例显示用户的个人资料名称和图像
如何在 IOS App 中使用 Facebook 登录缓存 Facebook 用户信息
如何使用 Facebook Graph API 和 facebook ios sdk 获取完整的用户对象?
希望对你有帮助......
您必须使用图形路径来获取用户信息。
-(void)getEmailId
{
[facebook requestWithGraphPath:@"me" andDelegate:self];
}
- (void)openSession
{
if (internetActive) {
NSArray *permissions=[NSArray arrayWithObjects:@"read_stream",@"email",nil];
[FBSession openActiveSessionWithReadPermissions:permissions allowLoginUI:YES completionHandler:
^(FBSession *session,
FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
}else
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"" message:@"Internet Not Connected" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}
}
#pragma mark- request delegate methods
- (void)request:(FBRequest *)request didLoad:(id)result
{
NSLog(@"request did load successfully....");
// __block NSDictionary *dictionary=[[NSDictionary alloc] init];
if ([result isKindOfClass:[NSDictionary class]]) {
NSDictionary* json = result;
NSLog(@"email id is %@",[json valueForKey:@"email"]);
NSLog(@"json is %@",json);
[[NSUserDefaults standardUserDefaults] setValue:[json valueForKey:@"email"] forKey:@"fbemail"];
[self.viewController login:YES];
}
}
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error
{
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"" message:@"Server not responding.." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alertView show];
[self fblogout];
[self showLoginView];
NSLog(@"request did fail with error");
}
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen: {
//state open action
}
// Initiate a Facebook instance
facebook = [[Facebook alloc]
initWithAppId:FBSession.activeSession.appID
andDelegate:nil];
// Store the Facebook session information
facebook.accessToken = FBSession.activeSession.accessToken;
facebook.expirationDate = FBSession.activeSession.expirationDate;
if (![[NSUserDefaults standardUserDefaults] valueForKey:@"fbemail"]) {
[MBProgressHUD showHUDAddedTo:self.viewController.view animated:YES];
[self getEmailId];
}
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
// Once the user has logged in, we want them to
// be looking at the root view.
[self.navController popToRootViewControllerAnimated:NO];
[FBSession.activeSession closeAndClearTokenInformation];
facebook = nil;
[self showLoginView];
break;
default:
break;
}
[[NSNotificationCenter defaultCenter]
postNotificationName:FBSessionStateChangedNotification
object:session];
if (error) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}
Facebook 目前已将其 SDK 版本更新为 4.x。要获取用户的个人资料信息,您需要在登录成功后显式调用图形 API(请求所需的权限)。
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:@"/me"
parameters:@{ @"fields": @"id,name,email"}
HTTPMethod:@"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
// Insert your code here
}];