0

I have designed a login app. I have entered the account data in sqlite database. However when I logged in with the account username and password i want these to be accessible to all view controllers. Because, I have different controllers which will update the account details. Need implementation details.. Thanks

user2741735
  • 41
  • 1
  • 6

5 Answers5

2

Use a singleton: http://www.galloway.me.uk/tutorials/singleton-classes/

I have a project with something like this (note I'm using the iOS keychain rather than a database, but I've hidden those details here):

@interface User : NSObject
@property (nonatomic, strong) NSString *username;

+ (User *)currentUser;

@end

@implementation User

+ (User *)currentUser
{
    static User *currentUser = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        currentUser = [[User alloc] initFromKeychain];
    });
    return currentUser;
}
@end

There are A LOT of approaches to doing singletons (depending on how paranoid you want to be about whether someone can create a non-singleton instance of your class or not). Examples here: What should my Objective-C singleton look like?

You could also just access the keychain directly wherever you need it. You should also ask yourself why so much of your code needs access to a username/password. Sounds like a broken design.

Community
  • 1
  • 1
ksimons
  • 3,797
  • 17
  • 17
1

Avoid global variables, rather you can use a Singleton class to store application wide credentials information.

However keep in mind that the best way to store credentials in an app is using Apple Keychain, you can find the API reference on the Apple site.

aleroot
  • 71,077
  • 30
  • 176
  • 213
1

As it was said - avoid global variables, but if you want them for sure - do it like in c language:

define this variable in one file

in all other files refer to it with extern keyword

Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
1

Simply: Don't do it!

I recommend to store the credentials in the keychain. But don't use the sample code Apple provides. It doesn't work. I use SSKeychain in my projects.

If you don't need to store the credentials you should, as aleroot suggested, use a singleton or a class with only one instance.

dasdom
  • 13,975
  • 2
  • 47
  • 58
0

Three answers:

  • Don't do it.
  • If you must do it, use a Singleton.
  • For extremely easy access to username and non confidential (i.e., password) variables, use NSUserDefaults.
ArtSabintsev
  • 5,170
  • 10
  • 41
  • 71
  • Thanks all for the quick suggestions. I'm actually needing the username to be accessed between controllers. So once the user has logged in with his username(and password), depending on the username (chosen from database)the user would be given certain privileges. How to go about this? – user2741735 Oct 19 '13 at 10:26
  • Use NSUserDefaults: https://developer.apple.com/library/mac/documentation/cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html An NSUserDefault object persists through app shutdown and app update/upgrade. Just make sure to clear that object if your user logs out. – ArtSabintsev Oct 20 '13 at 03:29