从Facebook登录xcode获取用户数据?

时间:2012-12-15 06:38:25

标签: iphone objective-c facebook

我已经在我的app中集成了facebook登录。但我无法获取用户数据,即它没有进入用户详细信息循环 在Appdelegate.m

- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
    NSArray *permissions = [[NSArray alloc] initWithObjects:
                            @"email",
                            @"user_likes",
                            nil];
    return [FBSession openActiveSessionWithReadPermissions:permissions
                                              allowLoginUI:allowLoginUI
                                         completionHandler:^(FBSession *session,
                                                             FBSessionState state,
                                                             NSError *error) {
                                             [self sessionStateChanged:session
                                                                 state:state
                                                                 error:error];
                                         }];





}


- (void)sessionStateChanged:(FBSession *)session
                      state:(FBSessionState) state
                      error:(NSError *)error
{
    switch (state) {
        case FBSessionStateOpen:
                     if (!error) {
                // We have a valid session
                NSLog(@"User session found");


                         [[FBRequest requestForMe] startWithCompletionHandler:
                          ^(FBRequestConnection *connection,
                            NSDictionary<FBGraphUser> *user,
                            NSError *error) {
                                  if (!error) {

                                  NSLog(@"%@",user);



                              }}];
                           break;
        case FBSessionStateClosed:
        case FBSessionStateClosedLoginFailed:
           [[FBSession activeSession] closeAndClearTokenInformation];

            break;
        default:
            break;
    }
 if (error) {
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Error"
                                  message:error.localizedDescription
                                  delegate:nil
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];
    }

    }
}

但是在会话打开的时候我找不到用户NSLog(@“%@”,用户)的NSLog打印bcoz NSLog(@“发现用户会话”)打印 是的,是吗?

1 个答案:

答案 0 :(得分:0)

我有同样的问题,我在一小时后登录后无法进入该区块我发现我正在返回mysession类而不是mysession实例。这可能会解决您的问题:

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {

    return [mysession handleOpenURL:url];
}