开发者

iPhone object that exists for entire app 'session'

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 tha开发者_如何学JAVAt 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:

#import <Foundation/Foundation.h>

@interface LoginClass : NSObject {
    BOOL userIsLoggedIn;
}
@property (nonatomic) BOOL userIsLoggedIn;

-(BOOL)checkIfUserIsLoggedIn;

@end


// Implemenatation
#import "LoginClass.h"

@implementation LoginClass
@synthesize userIsLoggedIn;
- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
        userIsLoggedIn = NO;
    }

    return self;
}

-(BOOL)checkIfUserIsLoggedIn
{
    return userIsLoggedIn;
}

@end

To set the login state, simply do something like this:

[[yourAppDelegateVar instanceOfLoginClass] setUserIsLoggedIn:YES];

or

[[yourAppDelegateVar instanceOfLoginClass] setUserIsLoggedIn:NO];


You can use the Singleton pattern, which is totally suitable for your problem.

Sure, you may use the AppDelegate for that as suggested below (as UIApplication is itself a singleton and therefore the AppDelegate -- the object that is the delegate of your UIApplication singleton -- only exists in one instance too) but is it better practice to keep the AppDelegate only for what it is designed for (i.e. the delegate of the UIApplication, namely managing everything that is related to app events like going to the background or back to the foreground, launching, receiving push notifs, and so on) to keep code clear and readable.

You better use another dedicated singleton for each Service you need to implement: this is better design practice. For example implementing a LoginService singleton class (See here in the Apple doc for implementation details) that will hold the -(BOOL)loginWithUsername:(NSString*)username password:(NSString*)password; method (that checks that your login/pwd is OK, potentially communicating with your WebService for this), a @property(readonly) BOOL loggedIn; property, probably a @property(readonly) NSString* currentUsername; if needed, and so on.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜