I am relatively new to iPhone development so please excuse what I hope is an easy task/question.
I have an app with a tab bar with several views each controlled by a MVC group. On loading the app the first mvc group of the tabbar gets displayed. In the viewDidAppear method of this first view controller I have a login screen modally displayed like so:
- (void ) viewWillAppear:(BOOL)animated
{
LoginViewController *loginvc = [[LoginViewController alloc] init];
loginvc.delegate = self;
if (*xxxxx*) {
[loginvc setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentModalViewController:loginvc animated:NO];
}
[loginvc release];
}
For the conditional if statement I would like a method to be called which checks that the user is logged in. I have provisionally (just for testing purposes) placed this method in the loginvc view controller however this cannot be a long term solution as the loginvc gets dealloc-ed once its dismissed (its the modal view).
At the moment I have a class set up like the following but don’t know if this is correct, and don’t know where to instantiate it (should this be in my main app delegate method application:didFinishLaunchingWithOptions: ?), and don’t know how to change the userIsLoggedIn variable:
LoginClass.h file
#import <Foundation/Foundation.h>
@interface LoginClass : NSObject {
BOOL userIsLoggedIn;
}
-(BOOL)checkIfUserIsLoggedIn;
@end
LoginClass.m file
#import "LoginClass.h"
@implementation LoginClass
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
userIsLoggedIn = NO;
}
return self;
}
-(BOOL)checkIfUserIsLoggedIn
{
return userIsLoggedIn;
}
@end
My question is how should I create an object of this class which exists for the whole time the application is alive. This is so that when the app is launched, the login screen is displayed, but after successful login the ‘userIsLoggedIn’ variable gets set to ‘YES’ so that if the first mvc is called again the login screen doesn’t get displayed modally again.
I hope I have made sense and hope someone can help with the code. If I am approaching this wrong please let me know alternative strategies (for example should I even be calling the modal display from the viewDidAppear of the first mvc?).
Thanks in advance,
Andy
The App Delegate is the place where your Login class should be. It’s always reachable by your view controllers and “lives” for the whole time your app is running. Check the docs to see how to access your App Delegate from your view controllers.
Your Login Class should also look like this:
To set the login state, simply do something like this:
or