如何在ios sdk中使用AccountIfPresent获取facebook用户和家乡

时间:2015-05-04 07:58:43

标签: ios iphone facebook-graph-api

- (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];
             }
         }
     }];
}

}

1 个答案:

答案 0 :(得分:0)

权限需要审核吗?

  • 要求三个基本权限不需要审核: public_profile,user_friends和email。

  • 当人们登录时,需要审核以询问任何其他权限 进入你的应用程序

您需要先通过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"];

希望这会对你有所帮助。