2

I'm not sure what the correct pattern for this situation is, so I'm not really sure what to search for here...

Basically, I want to have my iOS (iPhone) application start with a login page always. From there, I need the rest of the application's ViewControllers to be able to access the login information, and I'm not sure what the best way to do this is...

  • Do I create some form of global variable somehow?
  • Do I pass in an instance of the class to each ViewController and have a property for each ViewController to hold this login information?
  • Do I store some token in the cache/temp/appfolder somehow that each one can read from to get login information?
rmaddy
  • 314,917
  • 42
  • 532
  • 579
michael
  • 14,844
  • 28
  • 89
  • 177

4 Answers4

5

You could go with a shared instance of an object that holds the users credentials. You could also put it in a property or object in your app delegate or you could write it to disk (either NSUserDefaults or a special file).

For sensitive login credentials however, the recommended thing would be to use the keychain for persistence.

I think I would go with a combination of keychain for persisting the data and a shared instance or class method to retrieve it throughout the app.

Mario
  • 4,530
  • 1
  • 21
  • 32
  • +1 for mentioning alternatives to NSUserDefaults for sensitive data concerns. – ninehundreds Jun 19 '13 at 17:06
  • It's better to create this shared instance as singletone. using appdelegate for this could cause problems with reading/debugging code. and you can accidentally create two of this instances. also this leads to importing appdelegate in every class – Roma Jun 19 '13 at 17:07
  • @Roma I don't agree. We don't know enough about any other requirements for the app. But to just say a singleton would be better or access through appDelegate would be causing problems is wrong in my opinion. It might be - but it is not generally true. – Mario Jun 19 '13 at 17:13
  • I agree with Mario. Keychain is probably the best bet if you are 'concerned' about security. There is an in-depth tutorial on the Ray Wenderlich website on how to use the keychain. http://www.raywenderlich.com/6475/basic-security-in-ios-5-tutorial-part-1 – sangony Jun 19 '13 at 17:37
2

There is concept called NSUserDefaults in iOS to which is used to store light weight information in iPhone memory.

Look at this examples to learn storing and retrieving data in NSUserDefaults : example1 and example2.


EDIT :

As you want to store login and password, use keychain. Here is an keychain example.

Community
  • 1
  • 1
Geek
  • 8,280
  • 17
  • 73
  • 137
0

You can use NSUserDefaults to store the username/password, but if you just need the login credentials or a token to make new service calls, and you usually send them on the headers of an HTTP request, I would just set them on the headers of your service classes, so you don't really need to store it anywhere.

If you want to store the info from a login response (like name, email, etc), I usually have a Constant class in my projects to keep global references and other constants (I don't like to keep stuff in the AppDelegate and have it included on every class), and I have a User class with all the user info. I keep a reference of that User object on the Constant class. It's simpler than having to pass it down to every ViewController in your app.

rantunes
  • 376
  • 4
  • 7
0

This is a simple, yet useful approach:

Use NSUserDefaults for storing a BOOL named didAuthUser

Whenever you want show a view controller that needs authentication check for didAuthUser (this is usually the root view controller) like this:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if ([prefs boolForKey:@"didAuthUser"]) {
    //Get stored credentials
}else{
    //Show login view controller
}

So, when a user starts using yout app, you'll be able to show a log in view controller. If the user has already logged in, you must check the stored credentials (use the keychain, do not store sensitive information in NSUserDefaults)

Using the keychain is not a very straightforward process, you may want to use something like this

If you use sskeychain, a simple implementation would use the following methods:

+ (BOOL)setPassword:(NSString *)password forService:(NSString *)serviceName account:(NSString *)account;

+ (NSString *)passwordForService:(NSString *)serviceName account:(NSString *)account;

Note that the method for retrieving the password asks for an account, you may use NSUserDefaults for this. It's ok to store the account on NSUserDefaults as long as the password is secure on the keychain.

This way you can access the login data all across the app.

Happy coding!

Eduardo
  • 1,383
  • 8
  • 13