I did it as follows.
First, you create something called LoginViewController. It implements your login logic. Your root controller is UITabBarController.
Now in AppDelegate:
- (void) doLogin:(NSDictionary *)dict
{
TransactionsViewController *transactions;
BalancesViewController *balances;
ProfileViewController *profile;
UINavigationController *navi;
UITabBarController *root;
root = (UITabBarController *)self.window.rootViewController;
navi = [root.viewControllers objectAtIndex:0];
transactions = [navi.viewControllers objectAtIndex:0];
transactions.delegate = self;
navi = [root.viewControllers objectAtIndex:1];
balances = [navi.viewControllers objectAtIndex:0];
balances.delegate = self;
navi = [root.viewControllers objectAtIndex:2];
profile = [navi.viewControllers objectAtIndex:0];
profile.delegate = self;
[transactions loadDataFromLogin:dict];
[balances loadDataFromLogin:dict];
[profile loadDataFromLogin:dict];
}
- (void) showLoginView
{
assert(loginController == nil);
assert(activityView == nil);
UITabBarController *tabbar = (UITabBarController *)self.window.rootViewController;
loginController = [tabbar.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
loginController.delegate = self;
[tabbar presentModalViewController:loginController animated:YES];
}
- (void) loginWithLogin:(NSDictionary *)dict relogin:(BOOL)relogin
{
NSString *_login = [dict valueForKey:@"email"];
NSString *_pass = [dict valueForKey:@"pass"];
NetworkOperation *op = [NetworkOperation operationLogin:_login pass:_pass];
[NetworkOperation enqueueOperation:op observer:self];
if (!relogin && !loginController)
[self doLogin:dict];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
session = nil;
loginController = nil;
return YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
NSString *login = [[NSUserDefaults standardUserDefaults] stringForKey:@"email"];
NSString *pass = [[NSUserDefaults standardUserDefaults] stringForKey:@"pass"];
NSString *name = [[NSUserDefaults standardUserDefaults] stringForKey:@"name"];
if ((login == nil || login.length == 0) ||
(pass == nil || pass.length == 0) ||
(name == nil || name.length == 0)) {
if (!loginController) {
[self.window makeKeyAndVisible];
[self performSelector:@selector(showLoginView) withObject:nil afterDelay:0.1];
}
} else {
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:login, @"email",
pass, @"pass", name, @"name", nil];
[self loginWithLogin:dict relogin:(session != nil)];
}
}
So your app delegate, checks if you have your pass and login stored. If yes then it uses them to login and switches to your root view controller. In my case it is tabbed one to show couple tabs with balances, transactions, etc.
If there is no login and pass then show login view controller on top of your root view controller.
Here @session is your current session. Login is using enqueued operations as you see in the code.
If you need more details, just ping me.