繁体   English   中英

在iOS 5中获取访问令牌后获取Facebook用户配置文件数据

[英]get Facebook user profile data after getting access token in iOS 5

我正在开发一个iOS 5应用程序,我需要在成功登录后获取用户个人资料数据。

现在,当用户成功登录时,我将获得访问令牌。

现在我试图让Facebook使用该访问令牌登录用户配置文件数据。

我试图关注Facebook开发者网站上提供的示例应用程序。

我没有使用FBconnect类型的资源文件。 我从示例应用程序添加“facebookSDK.framework”。

请帮我。

谢谢,

- (void)populateUserDetails {

FBRequestConnection *connection = [[FBRequestConnection alloc] init];

// First request uploads the photo.
FBRequest *request1 = [FBRequest requestForMe];


if (FBSession.activeSession.isOpen) {

    [connection addRequest:request1
         completionHandler:
     ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
         if (!error) {

             NSLog(@"fb user name = %@",user.name);
             NSLog(@"fb user name = %@",user.id);
         }

         NSLog(@"fb2 user name = %@",user.name);
         NSLog(@"fb2 user name = %@",user.id);
     }
            //batchEntryName:@"photopost"
     ];

    [connection start];
}

}

在你的.h

#import<FacebookSDK/FacebookSDK.h>

现在在你的.m文件中只需调用以下方法来获取用户数据

-(void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user{
    NSLog(@"usr_id::%@",user.id);
    NSLog(@"usr_first_name::%@",user.first_name);
    NSLog(@"usr_middle_name::%@",user.middle_name);
    NSLog(@"usr_last_nmae::%@",user.last_name);
    NSLog(@"usr_Username::%@",user.username);
    NSLog(@"usr_b_day::%@",user.birthday);

  }

上面的方法在FBLoginView.h中定义,你可以在那里看到。

这个方法我添加了根据最新的facebookSDK.framework.Hope它会帮助你。

这是获取用户个人资料图片的方法:

//in your app delegate file configure "FBProfilePictureView" class
- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    [FBProfilePictureView class];
}

//in your header file
IBOutlet FBProfilePictureView   *userPictureView;


@property (strong, nonatomic) FBProfilePictureView     *userPictureView;

//here "userPictureView" is uiview not uiimageview

//in your .m file 

-(void)setProfilePicture {
    [FBSession.activeSession closeAndClearTokenInformation];

    NSArray *permissions = [NSArray arrayWithObjects:@"email", nil];

    [FBSession openActiveSessionWithReadPermissions:permissions
                                       allowLoginUI:YES
                                  completionHandler:
     ^(FBSession *session,
       FBSessionState state, NSError *error) {
         NSLog(@"\nfb sdk error = %@", error);
         switch (state) {
             case FBSessionStateOpen:
                 [[FBRequest requestForMe] startWithCompletionHandler:
                  ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
                      if (!error) {

                          self.userPictureView.profileID = [user objectForKey:@"id"];

                      } 
                  }];
                 break;
             case FBSessionStateClosed:
                 //need to handle
                 break;
             case FBSessionStateClosedLoginFailed:
                 //need to handle
                 break;
             default:
                 break;
         }
     }];
}

如果您编写完成处理程序以将结果视为FBGraphObject,我发现它最有效:

[[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, FBGraphObject *result, NSError *error) {
  if (result && !error) {
    name = result[@"name"];
    email = result[@"email"];
    facebookId = result[@"id"];
  } else {
    NSLog(@"Error!");
  }
}];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM