I am developing an application using Storyboards. This application requires the user to be logged in.
The Storyboard has a Login View as initial. When login is ok, it performs a segue to a TabViewController.
On any call to the API, I check if the server returns 401 (not-authorized). If this happens, a boolean is set to false (boolean isLogged). AppDelegate observes this boolean. If value is changed to false, I want to return the user to the login screen (remember, the initial view on Storyboard).
Here is some code:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"isLogged"]) {
BOOL logged = [[change objectForKey:NSKeyValueChangeNewKey] boolValue];
if (logged) {
NSLog(@"Logged in succesfully!");
} else {
NSLog(@"Logout performed");
[self.window makeKeyAndVisible];
[self.window layoutSubviews];
}
}
This works fine, but when I try to login again LoginOK segue is not performed.
I have tried many other options, something like this:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *loginController = [storyboard instantiateViewControllerWithIdentifier:@"Login"];
[source presentModalViewController:loginController animated:YES];
But the problem is that I don't know which View Controller performed the logout. Source has to be the current View Controller shown in the application, isn't it?