使用iOS FacebookSDK对用户进行身份验证会将用户注销

时间:2012-09-24 21:30:44

标签: ios facebook authentication facebook-ios-sdk

我正在使用FB iOS SDK来获取用户的凭据和访问令牌并保存它们。在之前的版本中,我设法正确地执行了此操作,但我的解决方案突然升级到iOS 6,因此我从Github下载了新版本,并根据说明编译并将框架添加到我的项目中。

我使用FB正确验证了我的用户,但是当浏览器(为auth提供的模态视图控制器)被解除时,我的用户将恢复到我的应用程序的初始视图,而不是从该程序启动的视图。

现在我在我的应用委托和我的SocialNetworksViewController中都有代码来执行此操作。

AppDelegate

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
  return [FBSession.activeSession handleOpenURL:url];
} 

- (void)sessionStateChanged:(FBSession *)session
   presentingViewController:(UIViewController *)presentingViewController
                      state:(FBSessionState) state
                      error:(NSError *)error
{
  switch (state) {
    case FBSessionStateOpen: {
      [presentingViewController dismissModalViewControllerAnimated:YES];
    }
      break;
    case FBSessionStateClosed:
    case FBSessionStateClosedLoginFailed:
      // Once the user has logged in, we want them to
      // be looking at the root view.

      [FBSession.activeSession closeAndClearTokenInformation];

      break;
    default:
      break;
  }

  [[NSNotificationCenter defaultCenter] postNotificationName:FBSessionStateChangedNotification
                                                      object:session];

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

- (void)openSessionFromViewController:(UIViewController *)viewController
{
  NSArray *permissions = [NSArray arrayWithObjects:@"publish_stream", @"email", @"user_birthday", nil];
  [FBSession openActiveSessionWithPermissions:permissions
                                 allowLoginUI:YES
                            completionHandler:
   ^(FBSession *session,
     FBSessionState state, NSError *error) {
     [self sessionStateChanged:session presentingViewController:viewController state:state error:error];
   }];
}

SocialNetworksViewController

- (IBAction)connectToFacebook:(UIButton *)sender {
  if (!self.facebookConnected) {
    AppDelegate <UIApplicationDelegate> *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [appDelegate openSessionFromViewController:self];
  }
}

- (void)sessionStateChanged:(NSNotification*)notification {

  NSLog(@"%@", FBSession.activeSession.accessToken);
}

- (void)facebookLoginFailed {
}

这也在viewDidLoad中:

[[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(sessionStateChanged:)
                                               name:FBSessionStateChangedNotification
                                             object:nil];

sessionStateChanged方法中,NSLog输出,然后我会发送到我的初始视图。

0 个答案:

没有答案