Facebook SDK 3.0:如何接收用户的电子邮件?
在“旧”FB iOS SDK中,我可以通过以下方式接收用户信息:
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"SELECT uid, name, email, pic FROM user WHERE uid=me()", @"query", nil]; JBFacebookManager *fbManager = [JBFacebookManager getInstance]; fbManager.requestDelegate = self; [fbManager.facebook requestWithMethodName:@"fql.query" andParams:params andHttpMethod:@"POST" andDelegate:self];
我如何用新的FB iOS SDK 3.0来做到这一点? 我是否需要使用FBRequest
或FBOpenGraphAction
或FBGraphObject
或这些或完全不同的组合?
if (FBSession.activeSession.isOpen) { [[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) { if (!error) { self.nameLabel.text = user.name; self.emailLabel.text = [user objectForKey:@"email"]; } }]; }
由于FBGraphUser
没有电子邮件FBGraphUser
,我们不能像名称(dot-syntax)那样访问信息,但NSDictionary仍然有电子邮件kv-pair,我们可以像访问正常的NSDictionary。
不要忘记要求电子邮件的许可,但:
NSArray *permissions = [[NSArray alloc] initWithObjects:@"email", nil]; [FBSession sessionOpenWithPermissions:permissions completionHandler: ^(FBSession *session, FBSessionState state, NSError *error) { [self facebookSessionStateChanged:session state:state error:error]; }];
一旦访问(id<FBGraphUser>)user
,您可以简单地使用user[@"email"]
。
从这里我们可以得到Facebook用户的基本信息和电子邮件ID。
[FBSession openActiveSessionWithReadPermissions:@[@"basic_info",@"email"] allowLoginUI:YES completionHandler:^(FBSession *session,FBSessionState status,NSError *error){ if(error) { NSLog(@"Facebook Error : %@",error.localizedDescription); } else{ } // Respond to session state changes, // ex: updating the view }];
你读过3.0 SDK的源代码吗? 有一种我认为是相同的方法:
- (FBRequest*)requestWithMethodName:(NSString *)methodName andParams:(NSMutableDictionary *)params andHttpMethod:(NSString *)httpMethod andDelegate:(id <FBRequestDelegate>)delegate;
login后获取信息的最简单的方法是:
-(IBAction)loginWithFacebook:(id)sender{ FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init]; [login logInWithReadPermissions: @[@"public_profile",@"email"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) { if (error) { NSLog(@"Process error"); } else if (result.isCancelled) { NSLog(@"Cancelled"); } else { NSLog(@"Logged in"); [self getFacebookProfileInfos]; } }]; } -(void)getFacebookProfileInfos { NSDictionary *parameters = @ {@"fields": @"id, name, first_name, last_name, picture.type(large), email"}; if ([FBSDKAccessToken currentAccessToken]) { [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:parameters] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { if (!error) { NSLog(@"fetched user:%@", result); } }]; }
}
你会得到所有的信息结果。
- FBAudiencenetworking:objc_boxable属性只适用于结构或联合
- 使用Iphone应用程序发布在朋友的脸书墙上
- Facebook iPhone SDK,应用程序在iOS 4.2以前崩溃,但在iOS 4.2+中运行良好
- FBSDKSharingcallback不返回结果
- Facebook API分页不起作用
- 如何使像Facebook的iOS应用程序或tumblr应用程序美丽的Tableview?
- Facebook对话框失败,错误:操作无法完成。 (NSURLErrorDomain错误-999。)
- 如何从Facebookgraphicsapi iOS获得具有特定ID的图像?
- 使用iOS SDK将照片发布到Facebook粉丝页面