• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 760
  • Last Modified:

saving login state

Hi all,

I'd just like to make it clear that I'm new to iphone development so still learning how to do stuff.

Anyway I have a main view which has the main menu for my app. On viewDidAppear I have it immediately displaying a login view modally. You can then put your login info in and it sends that info via http post, does a check to see if the user exists, if it does it returns the users unique id and maybe a bool to say they are logged in.

So whats the best way to store that user id and the login state in the app. Once that info is saved I can check against it during viewDidAppear to decide if the login view should be displayed.

Then when the app quits the login state is reset to not logged in.

I have all the http posting and server side stuff working correctly. I'm just not sure of the best way to save that the user is logged in.

Any help on the best way to achieve this would be great.
0
richard-mckenna
Asked:
richard-mckenna
  • 2
1 Solution
 
richard-mckennaAuthor Commented:
OK just a little update I now have this working using NSUserDefaults, again I dont know if this is the best way or not.

So on ApplicationDidFinishLoading I set the login state to NO

In viewDidAppear on my main view I check the login state, if no I display the login view.

once the user has been authenticated I set the login state to YES and dismiss the login view.
0
 
SpeedyApocalypseCommented:
You can use NSUserDefaults to save non-important information like a user ID.  However, it is visible if a user is jailbroken so you wouldn't want to save passwords or anything in there without encryption.  

Saving the state on DidFinishLaunching should be fine and you can also reset the state when the ApplicationDidClose.
0
 
gawhemantCommented:
Hello,
Check out following code .h and .m file....

"AccountManager.h"
#import <Foundation/Foundation.h>


@interface AccountManager : NSObject {

	NSString *accountUserName;
	NSString *accountPassword;
	NSString *UserID;
	BOOL isAuthenticated;
}

@property(nonatomic, retain)NSString *accountUserName;
@property(nonatomic, retain)NSString *accountPassword;
@property(nonatomic)BOOL isAuthenticated;

+(AccountManager *)sharedAccountManager;

-(void)resetUser;

-(void)saveUserDefaults;
-(void)loadUserDefaults;


@end

Open in new window



"AccountManager.m"

#import "AccountManager.h"

#define USERNAME	@"userName"
#define PASSWORD	@"password"
#define ISAUTHENTICATED		@"isLogOut"
#define USER_ID				@"userId"

@implementation AccountManager

@synthesize accountUserName, accountPassword, isAuthenticated;
@synthesize UserID;


AccountManager *sharedInstance = nil;

+(AccountManager *)sharedAccountManager
{
	
		if(sharedInstance == nil)
		{
			sharedInstance = [[AccountManager alloc] init];
			
		}
	return sharedInstance;
}

- (id) init
{
	self = [super init];
	if (self != nil) {
		self.isAuthenticated = NO;
	}
	return self;
}

-(void)resetUser
{
	self.isAuthenticated = NO;
	self.accountUserName = nil;
	self.accountPassword = nil;
	self.UserID = nil;
	
}

-(void)loadUserDefaults
{
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	self.isAuthenticated = [defaults boolForKey:ISAUTHENTICATED];
	self.accountUserName = [defaults valueForKey:USERNAME];
	self.accountPassword = [defaults valueForKey:PASSWORD];
	self.UserID = [defaults valueForKey:USER_ID];
	

}

-(void)saveUserDefaults
{
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	if(self.accountUserName != nil)
		[defaults setValue:self.accountUserName forKey:USERNAME];
	if(self.accountPassword != nil)
		[defaults setValue:self.accountPassword forKey:PASSWORD];
	[defaults setBool:self.isAuthenticated forKey:ISAUTHENTICATED];
	[defaults synchronize];
}
@end

Open in new window



With help of this you can maintain the UserId throughout your App.
With NSuserdefaults you can save them and use it anytime.And on resetting NSUserDefaults you will lose them.
Let me know if you want anymore info.
Regards,

Hemant
0
 
richard-mckennaAuthor Commented:
This is similar to what I was trying to do, but a much more elegant way of doing it, thanks for your help.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now