Search code examples
objective-ciosxcode4

Unknown error when trying to show login screen over my tab bar controller


My app has a tab based layout.

Before users can see the main screens from the main tab-bar controller though, they have to sign in first.

The tab-bar screens make requests to an external API and require a session ID so the tab-bar controller's sub controllers cannot be loaded before the user has signed in (otherwise the app crashes because the API requests are attempted without the session ID).

Here's what I have in my app delegate:

#import "AppDelegate.h"
#import "APIRequest.h"
#import "SignInController.h"
@implementation AppDelegate

@synthesize window = _window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:NO];
    // Override point for customization after application launch.

    NSString *sessionID = [[NSUserDefaults standardUserDefaults] stringForKey:UserDefaultsSessionIDKey];
    if (sessionID == (id)[NSNull null] || sessionID.length == 0){
        NSLog(@"session ID was null");

        UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;  
        SignInController *signInController = [tabBarController.storyboard instantiateViewControllerWithIdentifier:@"SignInScreen"];

        [self.window addSubview:signInController.view];
        [self.window makeKeyAndVisible];

        }
    return YES;
}

When I try this, the login screen is not shown. Instead, the tab bar controller's first screen is loaded and the app abends because of the lack of session ID as mentioned above.

Can anyone advise why the sign in screen isn't being loaded?


Solution

  • Why not put the logic to determine whether someone is logged in inside the first VC? I do this same process in several applications and my model is to check to see if the user is logged in when I try to load data (my own custom method). If this fails, then the user is sent to a modally presented LoginVC that has a delegate method inside the first VC (that method passes back the created user object).