-1

I am a newbie to iOS development.

How and where to store values we pull from the server at the time of authentication? I am developing this app which uses a login screen. Once you try to login, it sends a JSON post to the API and the server responds. We get back a unique userid. How and where should I store that unique user id so that I can use it to pull data across different views? That is after loggin in, it goes to a different view controller. Is there anything like a global variable or storage which I can access in any view controller?

Thanks.

  • NSUserDefaults would do that for you: `[NSUserDefaults StandardUserDefaults]setObject: userid forKey :@"userid"];` – Robert Jul 01 '13 at 11:23

4 Answers4

1

You can store your data in dictionary ,Then you can use the User Defaults to pass the values like this

NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];


NSArray *dataArr=[data JSONValue];

for (int i=0; i<[dataArr count]; i++) {

    NSDictionary *dict=[dataArr objectAtIndex:i];


    NSString *codeV=[dict valueForKey:@"userid"];
   [NSUserDefault StandasdUserDefault]setObject: dict forKey :@"logindata"];
    NSLog();
}

    NSLog(@"%@", json_string);

Another view

-(void) ViewDidLoad{

  NSDictionary *dict = [NSUserDefault StandardUserDefault]objectForKey:@"logindata"]
NsLog(@"dict is %@",dict);
}
Purva
  • 1,301
  • 2
  • 15
  • 30
0

You can use NSUserDefaults for this. A defaults database is created automatically for each user. we can save floats, doubles, integers, Booleans, UrLs, etc. We can use following methods to set and retrieve:

To set :

- (void)setInteger:(NSInteger)value forKey:(NSString *)defaultName

To retrieve:

- (NSInteger)integerForKey:(NSString *)defaultName

The complete description of NSUserDefaults is here: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html

Puneet Sharma
  • 9,169
  • 1
  • 25
  • 31
0

You can use NSUserDefaults, Keychain or Singleton provided by people. But it is depends on your app.

Do you want keep data all the time? Even app is killed? Then NSUserDefaults and Keychain (more secure) would be fine. Singleton will lose data when app is killed.

But what I more interest in is your app itself. For your web service, does it only need user id? No session? or other things that can prevent people call your web service if they know the address and id and they have no idea about login id and password?

brianLikeApple
  • 4,111
  • 1
  • 24
  • 45
0

There are 2 scenarios

1. You want to save the value across multiple run of the application

That means once you got the userId, you need to save it, so that next time user runs the app, there is no need of login.

DON'T USE NSUserDefault IN THIS SCENARIO

to save the value. If your userID (or username or password) are sensitive information and NSUserDefault stores the data in plain text. Anyone can easily retrieve the information from the application bundle.

In this scenario always use KeyChain to save data. You can also read why keychain is preferred over NSUserDefault.

2.You only need to save the value during application's current run.

In this scenario, forget NSUserDefault, forget KeyChains, just create a property in your application delegate file, which can be accessed from anywhere inside your app.

In short, Never use NSUserDefault to store sensitive information (like password, card numbers etc). Brilliant explained here.

Community
  • 1
  • 1
Krishnabhadra
  • 33,955
  • 30
  • 116
  • 165