2015-05-04 84 views
0
- (IBAction)loginbtnclicked:(id)sender 
{ 
if (!FBSession.activeSession.isOpen) 
{ 
    [FBSession.activeSession openWithBehavior:FBSessionLoginBehaviorUseSystemAccountIfPresent 
          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) 
     { 
      [FBSession openActiveSessionWithReadPermissions:@[@"email",@"user_location",@"user_birthday",@"user_hometown"] 
               allowLoginUI:NO 
              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) 
        { 
         [FBSession setActiveSession:session]; 
         [self loginbtnclicked:sender]; 
        } 

       }]; 
     } 
    }]; 
    return; 
} 

    [FBRequestConnection startWithGraphPath:@"me" parameters:[NSDictionary dictionaryWithObject:@"picture,id,birthday,email,name,gender,hometown,about" forKey:@"fields"] HTTPMethod:@"GET" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) 
    { 
     if (!error) 
     { 
      if ([result isKindOfClass:[NSDictionary class]]) 
      { 
       if([result objectForKey:@"data"]) 
        dictionary = (NSDictionary *)[(NSArray *)[result objectForKey:@"data"] objectAtIndex:0]; 
       else 
        dictionary = (NSDictionary *)result; 
       user_email = [dictionary objectForKey:@"email"]; 
       [dictionary retain]; 


       accessTokan = [[[FBSession activeSession] accessTokenData] accessToken]; 

       NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
       [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/me?access_token=%@",accessTokan]]]; 
       [request setHTTPMethod:@"GET"]; 
       [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
       NSError *error; 
       NSURLResponse *response; 
       NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
       NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]; 

       fb_dict = [str JSONValue]; 
       [str release]; 
       NSLog(@"fb dict %@",fb_dict); 
       [fb_dict retain]; 
       [self getfriends]; 
      } 
     } 
    }]; 
} 

}如何获取关于Facebook的用户和家乡使用AccountIfPresent在IOS SDK

回答

0

权限需要经过审核?

  • 审查要求的三个基本权限并不需要: public_profile,user_friends和电子邮件。

  • 审查需要询问任何其他权限,当人们登录 到您的应用程序

您需要通过Facebook来要求你的应用程序的审查,才能公开使用它们。见Facebook开发人员link

后您的应用程序获得批准: -

获取用户约和家乡就在现有的代码添加此行: -

[FBRequestConnection startWithGraphPath:@"me" parameters:[NSDictionary dictionaryWithObject:@"picture,id,birthday,email,name,gender,hometown,about" forKey:@"fields"] HTTPMethod:@"GET" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) 
    { 
     if (!error) 
     { 
      if ([result isKindOfClass:[NSDictionary class]]) 
      { 
       if([result objectForKey:@"data"]) 
        dictionary = (NSDictionary *)[(NSArray *)[result objectForKey:@"data"] objectAtIndex:0]; 
       else 
        dictionary = (NSDictionary *)result; 
       user_email = [dictionary objectForKey:@"email"]; 
       [dictionary retain]; 


       accessTokan = [[[FBSession activeSession] accessTokenData] accessToken]; 

       NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
       [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/me?access_token=%@",accessTokan]]]; 
       [request setHTTPMethod:@"GET"]; 
       [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
       NSError *error; 
       NSURLResponse *response; 
       NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
       NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]; 

       fb_dict = [str JSONValue]; 
       //Here you will fetch the user about and hometown 


       NSString *str_HomeTown=[fb_dict objectForKey:@"hometown"][@"name"]; 

       NSString *str_About=[fb_dict objectForKey:@"bio"]; 

希望这将帮你。

+0

我已经采取了所有许可 –

+0

检查我的更新答案 – Vizllx

+0

但是当我用这个fBSessionLoginBehaviorUseSystemAccountIfPresent则无法获得有关和家乡 –

相关问题