1

我正在使用 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输出然后我被发送到我的初始视图。

4

0 回答 0