25

在“旧”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 做到这一点?我是否需要使用FBRequestFBOpenGraphActionFBGraphObject或这些或完全不同的东西的组合?

4

5 回答 5

68
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没有 email @property,我们无法访问名称(点语法)等信息,但是 NSDictionary 仍然有 email 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];
 }];
于 2012-08-07T10:48:52.307 回答
5

一旦您可以访问(id<FBGraphUser>)user,您可以简单地使用 , user[@"email"]

于 2013-10-22T00:04:59.310 回答
1

你读过3.0 SDK的源代码吗?有一种方法我认为是相同的:

- (FBRequest*)requestWithMethodName:(NSString *)methodName
                          andParams:(NSMutableDictionary *)params
                      andHttpMethod:(NSString *)httpMethod
                        andDelegate:(id <FBRequestDelegate>)delegate;
于 2012-08-07T10:47:31.760 回答
1

从中我们可以得到 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

    }];
于 2013-08-22T12:12:01.550 回答
0

登录后获取信息的最简单方法是:

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

}

您将获得结果的所有信息。

于 2016-04-21T12:05:14.703 回答