?
Solved

saving login state

Posted on 2011-03-08
4
Medium Priority
?
758 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you looking to clear some space on your phone for the latest iOS 8 update? Did you switch to Spotify so you no longer need to keep music native on your phone? Run out of space for taking photos while in the middle of vacation? Sometimes the quic…
Is your phone running out of space to hold pictures?  This article will show you quick tips on how to solve this problem.
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.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

765 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