Solved

saving login state

Posted on 2011-03-08
4
753 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

     Intro      In this article, I will show you how to parse a JSON in Swift using an open source library called SwiftyJSON (https://github.com/SwiftyJSON/SwiftyJSON). If you haven't heard of a JSON before, it stands for JavaScript Object Notation…
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…
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…
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…

839 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