-1
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

 if ([defaults objectForKey:@"username"] == nil)
{
    //Load Login View if no username is found
    NSLog(@"No username found");
    self.nameLoginView=[[NewLoginViewController alloc]initWithNibName:@"NewLoginViewController" bundle:nil];
    [self.navigationController pushViewController:self.nameLoginView animated:YES];
}

else
{
    NSString *savedUsername = [defaults stringForKey:@"username"];
    NSLog(@"Username found: %@", savedUsername);

    self.mainView = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];

    [self.navigationController pushViewController:self.mainView animated:YES];

}

I mean if he is logging for first time in our app he has to go to newLoginView Controller and if user is logging for second time he has to go to another view controller? i am doing this code in view controller itself?

iDev
  • 1,042
  • 13
  • 19
chaithanya
  • 13
  • 6
  • 1
    Don't understand what you are asking. Can you describe a little bit. And your post is hard to read. – Lucas Huang Jan 14 '15 at 06:44
  • What do you mean by Old or New? – Adeel Ur Rehman Jan 14 '15 at 06:45
  • VTC as "unclear what you're asking" – Novarg Jan 14 '15 at 06:48
  • where u check this condition in **AppDeleagte** or **VIewController**, another ? -- self.prefs (what is this) – Anbu.Karthik Jan 14 '15 at 06:48
  • here is what i understand from your question. If the user has logged in first time he becomes new user and you are saving the credentials in `NSUSERDefaults` and calling a View Controller. If the user has already logged in then you are checking credentials with `NSUSERDefaults` and if it matches you are calling another viewcontroller. Correct me if i am wrong. – LeXeR Jan 14 '15 at 07:16
  • @lexer yaw you are right thats my question – chaithanya Jan 14 '15 at 07:24
  • What doesn't work with the code you show in the question? Which view controller is it in? – Wain Jan 14 '15 at 07:45

2 Answers2

1

Hope i am getting right your problem, you have problem to find that user is saved in your user Default or new user please have a look of below link:Click here to find solution

Hope this will be help full for you.

Community
  • 1
  • 1
Vinod Pandey
  • 183
  • 4
  • Thanks Vinod,i have tried same code,but i am not able to understand how to check user – chaithanya Jan 14 '15 at 07:31
  • `NSString *savedUsername = [defaults stringForKey:@"username"];` this will give you credentials from `NSUSERDefaults`. Now get the credentials that user used to login (You might be using `NSTextField` i presume) Compare with `(aEditText.text isEqualToString:savedUsername)` – LeXeR Jan 14 '15 at 07:41
  • You can use this::-- NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; NSObject * object = [prefs objectForKey:@"your_particular_key"]; if(object != nil) { //object is there } – Vinod Pandey Jan 14 '15 at 07:50
0
        NSString *userName = [[NSString alloc] init];
        NSString *userPass = [[NSString alloc] init];

        userName = [[NSUserDefaults standardUserDefaults] objectForKey:@"username"];
        userPass = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];

        if([userName isEqualToString:name.text] && [userPass isEqualToString:passcode.text])
        {
             authgrantflag=1;
        }

        // Compare entered combo with configurator stored values ---Offline Authentication //

        NSLog(@"Authentication flag=%d",authgrantflag);
        if(authgrantflag==1)
        {
            // Local authentication successfull call home page//
            NSLog(@"Local authentication success");
            [pop1 dismissPopoverAnimated:YES];

            HomeScreen *dcrmainpage=[[[HomeScreen alloc] initWithNibName:nil   bundle:nil]autorelease];
            [self presentViewController:dcrmainpage animated:YES completion:nil];

        }
        else
        {
          HomeScreen1 *dcrmainpage=[[[HomeScreen1 alloc] initWithNibName:nil bundle:nil]autorelease];
            [self presentViewController:dcrmainpage animated:YES completion:nil];
        }
LeXeR
  • 214
  • 3
  • 20