应用程序从 Facebook 登录返回后,我在关闭模式视图时遇到问题。我遵循了 facebook 教程并对代码进行了一些自由化,因为我不想使用应用程序委托。我会发布一些代码片段
我正在使用两个类。一个类是我的初始视图,如果用户已登录,则会弹出一个名为 DayViewController 的类,然后如果用户未登录,则会弹出我的模式视图。 LoginViewController。
还应该注意的是,我正在使用故事板,并且我构建了我的应用程序,以便 DayViewController 嵌入了一个导航栏,然后嵌入到选项卡栏中。我认为我的问题出在 facebook 教程中,他们使用一些代码来获取当前显示的视图,然后将其关闭……我无法实现。
DayViewController 类——
ViewDidLoad
- (void)viewDidLoad
{
if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
[self openSession];
}else {
[self showLoginView];
}
[super viewDidLoad];
}
显示登录视图
- (void)showLoginView{
UIViewController *topViewController = [self.navigationController topViewController];
UIViewController *modalViewController = [topViewController presentedViewController];
if (![modalViewController isKindOfClass:[LoginViewController class]]) {
LoginViewController *lvc = [self.storyboard instantiateViewControllerWithIdentifier:@"loginView"];
[self presentViewController:lvc animated:NO completion:nil];
} else {
LoginViewController *loginVC = [[LoginViewController alloc] init];
[loginVC loginFailed];
}
}
sessionStateChanged
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen: {
UIViewController *topViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"loginView"];
//LoginViewController *lvc = [self.storyboard instantiateViewControllerWithIdentifier:@"loginView"];
if ([[topViewController presentedViewController]
isKindOfClass:[LoginViewController class]]) {
[self dismissViewControllerAnimated:NO completion:nil];
}else{
}
}
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
// Once the user has logged in, we want them to
// be looking at the root view.
//[self.navController popToRootViewControllerAnimated:NO];
[FBSession.activeSession closeAndClearTokenInformation];
[self showLoginView];
break;
default:
break;
}
if (error) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
NSLog(@"Error");
[alertView show];
}
}
默认 OpenSession
- (void)openSession{
[FBSession openActiveSessionWithReadPermissions:nil allowLoginUI:YES completionHandler: ^(FBSession *session,FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
}
LoginViewController 类---
这个类没有什么戏剧性的,只有一个登录按钮,它通过 openSession 函数抛出 facebook 状态
- (IBAction)signInButton:(id)sender {
[self.loginActivityIndicator startAnimating];
dvc = [[DayViewController alloc] init];
[dvc openSession];
//[self dismissViewControllerAnimated:YES completion:Nil];
}