0

At the start of my application i make my users to register first then take them to login View.I want to save the successfully registered users and let them get to the loginView instead of Register View.

How can i do this.As far i know iphone is registered as one user.Tell me what is the best way.And how to keep the user data and redirect to login view for a user?

Thanks

sohel14_cse_ju
  • 2,481
  • 9
  • 34
  • 55

1 Answers1

0

You can use NSUserDefaults to save user data such as login info.

Take a look at this tutorial and the reference

In Short, you would do something like:

Saving after registration:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:@"UserName" forKey:@"UserName"];
[prefs setObject:@"Pass" forKey:@"Pass"];
// This is suggested to synch prefs, but is not needed
[prefs synchronize];

Retrieving on app start:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *userName = [prefs stringForKey:@"UserName"];
NSString *pass = [prefs stringForKey:@"Pass"];
Daniel
  • 20,420
  • 10
  • 92
  • 149
  • ok..i am making a try with this approach...will let you know what happens..thanks for reply.. – sohel14_cse_ju Nov 02 '11 at 12:34
  • Can you explain what the SetObject:@"UserName" doing here?I need to register a user with any name and save it on preference/log.And when opening app let him redirect to login page checking if this user is already register.I am little confused with my approch.Can you suggest me the best way? – sohel14_cse_ju Nov 02 '11 at 12:43
  • With this approach, you save the username and password of a registered user in NSUSerDefaults. And retrieve it at app start, or whenever you want to check if the user login info has been saved to user defaults. – Daniel Nov 02 '11 at 13:12
  • [prefs setObject:@"Max" forKey:@"UserName"]; would save the username Max with key UserName in the NSUSerDefaults. At the next start you can do something like `if([prefs stringForKey:@"UserName"]!=nil) sendToLoginWithUsername:[prefs stringForKey:@"UserName"]; else sendToRegister;` – Daniel Nov 02 '11 at 13:14
  • ok..can you tell me where it's saving the user information?can i check it? – sohel14_cse_ju Nov 03 '11 at 05:59
  • i made a try.But when i am checking if any user is registered it's not getting any user.I am testing on my simulator.I need to know where it saves the user information? – sohel14_cse_ju Nov 03 '11 at 09:07
  • See http://stackoverflow.com/questions/3178115/iphones-nsuserdefaults-is-a-database-or-plist It is getting no users because you probably didn't save any yet. Please, also read the reference I posted in the answer. – Daniel Nov 03 '11 at 10:36