Solved

saving login state

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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
BitDefender GravityZone for EndPoint, certificates for iOS. 1 59
Questions on iPhone 7 Plus to put best in use 14 57
iPad Testing Issue 4 63
HP 8210 Printer drops offline 8 66
The release of the iPhone 6 and the iPhone 6 Plus was extremely successful for Apple. However when iOS 8.1 was introduced, many iPhone users complained of quick battery drainage. Without even knowing, you may be unnecessarily using up your battery p…
Are you having trouble connecting or getting your iPhone / Samsung device(s) to sync with Microsoft Exchange Server?   What have you tried?   What haven't you tried?
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.
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…

756 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