-1

I am making a login system in objective c (for an iPhone app) and I would like to record if the user is logged in or not. I figured the best way to do this would be to use a global variable BOOL isLoggedIn after I have validated their credentials. Can someone please help me or give me some advice because I am completely lost on how to do this.

Thanks in advance

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Thomas
  • 2,356
  • 7
  • 23
  • 59
  • Did you try anyhting at all? There are couple of similar questions asked before. Read FAQs first. – rohan-patel Jun 03 '13 at 06:40
  • You can define one BOOL in .pch file of xcode project. that would be you global variable and could be accessible in round the project. – jogi47 Jun 03 '13 at 06:42
  • Don't go global should be contained in the answer - this is greatly discouraged in objective c design patterns – Mario Jun 03 '13 at 07:08
  • "BOOL isLoggedIn" - *Bad Idea*. You need to provide some type of binding. That is, don't depend on the `BOOL` flag; rather, bind authorization to authentication. To see why, see [Preventing Widespread Automated Attacks in iOS](https://viaforensics.com/iphone-forensics/preventing-widespread-ios-application-infection.html). – jww May 01 '14 at 10:08

3 Answers3

1
-(void)saveToUserDefaults:(BOOL)isLoggedIn
{
    NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];

    if (standardUserDefaults) {
        [standardUserDefaults setBool:isLoggedIn forKey:@"isLoggedIn"];
        [standardUserDefaults synchronize];
    }
}

Use NSUserDefaults, It will retain the value until your app is deleted from the phone.

Rajan Balana
  • 3,775
  • 25
  • 42
1

use this two function

   -(void)addToNSUserDefaults:(id)pobjValue forKey:(NSString *)pstrKey{
       NSUserDefaults *objUserDefaults = [NSUserDefaults standardUserDefaults];
       [objUserDefaults setObject:pobjValue forKey:pstrKey];
       [objUserDefaults synchronize];
   }

   -(id)fetchFromNSUserDefaults:(NSString *)pstrKey{
       NSUserDefaults *objUserDefaults = [NSUserDefaults standardUserDefaults];
       return [objUserDefaults objectForKey:pstrKey];
   }

When you store it

        [self addToNSUserDefaults:@"YES" forKey:@"isLoggedIn"];

When retrive it

       if ([self fetchFromNSUserDefaults:@"isLoggedIn"]==nil || [[self fetchFromNSUserDefaults:@"isLoggedIn"] isEqualToString:@""]) {
             //Go to Login
       }
       else 
           //all ready Login
SAMIR RATHOD
  • 3,512
  • 1
  • 20
  • 45
1

You can either use NSUserDefaults to store the app's login state as stated by the others. The value will stay as is until the app is deleted. i.e. even when you kill the app or restart the device, if the last value was 'logged in'.

If you want to 'reset' the user's state when the app is killed or device is restarted, then another approache is to make your User's object as a singleton. When app is killed or device is restarted, unlike the NSUserDefault, the user's state will be reset (as the object no longer exists) and user will need to login again. Using this method: http://lukeredpath.co.uk/blog/a-note-on-objective-c-singletons.html to create singleton of your user's object

+ (id)sharedInstance
{
  static dispatch_once_t pred = 0;
  __strong static id _sharedObject = nil;
  dispatch_once(&pred, ^{
    _sharedObject = [[self alloc] init]; // or some other init method
  });
  return _sharedObject;
}

Add the above to your User's class and then you can then add the appropriate properties related to the user's state, e.g BOOL isLoggedOn, and then you can access this along the line:

BOOL isLoggedIn = [[User sharedInstance] isLoggedIn];
maethorr
  • 594
  • 4
  • 7