我是iPhone开发的新手。我正在做一个用户通过Facebook登录2的应用程序,一旦他提交了他的凭据并点击登录按钮,我必须从Facebook获取诸如名字,电子邮件和性别的详细信息,并且在获取用户之后必须被定向到应用程序的注册页面填写了详细信息,我需要这个应用程序兼容iPhone 4,4s和5。
我尝试使用Facebook Graph API做到这一点,但无法得到它,所以任何人都可以帮助我。
先谢谢。
答案 0 :(得分:18)
您可以使用以下代码执行此操作:
[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日)
现在,Faceboook已发布了主要更改的新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);
}
}];
}
但在获取用户详细信息之前,我们必须按照Documentation here中的说明在我们的代码中集成新的Facebook登录信息。
Here 是SDK v4的更改日志。我建议通过它进行更新。
答案 1 :(得分:3)
添加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);
}
}
}
}];
}
答案 2 :(得分:1)
请参考以下链接,以便了解您的想法。
https://www.parse.com/tutorials/integrating-facebook-in-ios
Facebook iOS 6 - get user info
get Facebook user profile data after getting access token in iOS 5
Display a user's profile name and image through the Facebook instance for iOS
How to cache Facebook User Info with Facebook Login in IOS App
How do I get the full User object using Facebook Graph API and facebook ios sdk?
希望它会帮助你......
答案 3 :(得分:1)
答案 4 :(得分:1)
您必须使用图形路径来获取用户信息。
-(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];
}
}
答案 5 :(得分:1)
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);
}
}];
答案 6 :(得分:0)
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
}];