当用户在iOS中使用NSUserDefaults登录时,如何检查用户是旧还是新?

时间:2015-01-14 06:37:57

标签: ios login nsuserdefaults

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];

}

我的意思是,如果他第一次在我们的应用程序中登录,他必须转到newLoginView控制器,如果用户第二次登录,他必须转到另一个视图控制器?我在视图控制器本身中执行此代码?

2 个答案:

答案 0 :(得分:1)

希望我能解决您的问题,您有问题发现用户已保存在您的用户中默认或新用户请查看以下链接:Click here to find solution

希望这对你有所帮助。

答案 1 :(得分: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];
        }
相关问题