Solved

saving login state

Posted on 2011-03-08
4
749 Views
Last Modified: 2012-05-11
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
Comment
Question by:richard-mckenna
  • 2
4 Comments
 

Author Comment

by:richard-mckenna
ID: 35068399
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
 
LVL 13

Expert Comment

by:SpeedyApocalypse
ID: 35074938
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
 
LVL 3

Accepted Solution

by:
gawhemant earned 500 total points
ID: 35080020
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
 

Author Closing Comment

by:richard-mckenna
ID: 35117640
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

The Basics of Storyboard Storyboards were first introduced to developers in the release of iOS 5. They save the developer the trouble of designing each screen’s interface in a different file. The storyboard allows you to see the conceptual mockup o…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now