Ios 如何检查我的用户是否已登录,并使用NSUserDefaults转到特定的视图控制器

Ios 如何检查我的用户是否已登录,并使用NSUserDefaults转到特定的视图控制器,ios,objective-c,nsuserdefaults,Ios,Objective C,Nsuserdefaults,给每个Vc一个故事板标识符,然后执行此操作 #import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize User_Name,Password,Login_Label,Register_Label; - (void)viewDidLoad { [super viewDidLoad]; NSUserDefaults *defaults =

给每个Vc一个故事板标识符,然后执行此操作

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize User_Name,Password,Login_Label,Register_Label;

- (void)viewDidLoad
{

[super viewDidLoad];


NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

if (![defaults boolForKey:@"Registered"])
{

    NSLog(@"No User registered");


    // Login_Label.hidden = YES;



}else
{

    NSLog(@"User Registered");

    //Register_Label.hidden = YES;

    [self performSegueWithIdentifier:@"LoggedInPage" sender:self];

}



// Do any additional setup after loading the view, typically from a nib.

}



- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}



- (IBAction)LogIn_Button:(id)sender {

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

if ([User_Name.text isEqualToString: [defaults objectForKey:@"userName"] ] && [Password.text isEqualToString: [defaults objectForKey:@"password"]])

{

    User_Name.text = nil;

    Password.text = nil;

    [self performSegueWithIdentifier:@"LoggedInPage" sender:NULL];


}else
{

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"OOPS" message:@"Username or Password is in correct" preferredStyle:UIAlertControllerStyleAlert];


    UIAlertAction *action = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)

                             {

                                 //[self dismissViewControllerAnimated:YES completion:Nil];

                             }];

    [alert addAction:action];

    [self presentViewController:alert animated:YES completion:Nil];

}


}


- (IBAction)Register:(id)sender {


if ([User_Name.text isEqualToString:@""] || [Password.text isEqualToString:@""])
{

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"OOPS" message:@"You must complete all fields" preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *action = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)

                             {

                                 [self dismissViewControllerAnimated:YES completion:Nil];

                             }];

    [alert addAction:action];

    [self presentViewController:alert animated:YES completion:Nil];

} else
{


    //[self NewRegisterUser];


}


[self performSegueWithIdentifier:@"RegisterationPage" sender:self];

}



-(void) NewRegisterUser{

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];


[defaults setObject:User_Name.text forKey:@"userName"];


[defaults setObject:Password.text forKey:@"password"];


[defaults setBool:YES forKey:@"Registered"];


[defaults synchronize];


UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Success" message:@"You have registerd a new user" preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *action = [UIAlertAction actionWithTitle:@"Done" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)

                         {

                             [self dismissViewControllerAnimated:YES completion:Nil];

                             [self performSegueWithIdentifier:@"LoggedInPage" sender:self];



                         }];

[alert addAction:action];

[self presentViewController:alert animated:YES completion:Nil];


//[self performSegueWithIdentifier:@"LoggedInPage" sender:self];


}



@end
应用程序中的代理 检查userDefaults后,如果未注册,则实例化loginViewController并将其设置为rootViewController,否则实例化要显示的viewController并将其设置为rootViewController

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

 if ([defaults boolForKey:@"LoggedIn"]) 
 {    
     UIViewController*vc = [storyboard instantiateViewControllerWithIdentifier:@"homeID"];
     UINavigationController*nav = [[UINavigationController alloc] initWithRootViewController:vc];
     self.window.rootViewController = nav

  }else
  {

     UIViewController*vc = [storyboard instantiateViewControllerWithIdentifier:@"loginID"];
     UINavigationController*nav = [[UINavigationController alloc] initWithRootViewController:vc];
     self.window.rootViewController = nav
  }
}


此代码应转到appDelegate中的
didFinishLaunching with options
功能

如果需要,请尝试在swift版本中执行以下代码。我将用目标c编写。或者根据需要修改以下代码。在我的例子中,我在仪表板中使用tabbarcontroller

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

if (![defaults boolForKey:@"Registered"]) 
 {

    NSLog(@"No User registered");

   // Instantiate the LoginViewController

    NSString * storyboardName = @"MainStoryboard"; 
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
    UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"IDENTIFIER_OF_YOUR_VIEWCONTROLLER"];

   // Set it as the rootViewController

    self.window.rootViewController = vc;


   }else
  {

    NSLog(@"User Registered");

    //Instantiate the viewController that you would like a user to be taken to


    NSString * storyboardName = @"MainStoryboard"; 
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
    UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"IDENTIFIER_OF_YOUR_VIEWCONTROLLER"];

   // Set it as the rootViewController

    self.window.rootViewController = vc;

你面临的问题你的代码有什么问题?什么在起作用?什么不起作用?为什么要隐藏此
/[self-NewRegisterUser]您似乎没有清除数据或设置bool值来指示用户已注销,在您的应用程序上下文中,会话是什么意思app@Sh_Khan-这也是我们可以遵循的一个选项,但问题是用户未使用此方法
/[self-NewRegisterUser]仍然不工作@sh_khanDid您在appdelegateyes中尝试过它@Sh_Khansee编辑…。谢谢@Sh_Khan您的代码运行良好…您能将其转换为此代码中的@RamGot错误吗
         func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

                  UserTypeValidateCall()

                return true
            }


         //MARK:- Validating User Type
            func  UserTypeValidateCall()
            {
                guard let Usertype = UserDefaults.standard.string(forKey: "userName") else {
                    return
                }



                if(( Usertype) != nil)
                {

        window = UIWindow(frame: UIScreen.main.bounds)
                let storyboard = UIStoryboard(name: "Main", bundle: nil)
                let ContentViewController = storyboard.instantiateViewController(withIdentifier: "your vc name") as! SKTabBarViewController
                navController = UINavigationController(rootViewController: ContentViewController)
                self.window?.rootViewController = navController
                self.window?.makeKeyAndVisible()

        }
else
{

      window = UIWindow(frame: UIScreen.main.bounds)
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let ContentViewController = storyboard.instantiateViewController(withIdentifier: "SKLogInViewController") as! SKLogInViewController
            navController = UINavigationController(rootViewController: ContentViewController)
            self.window?.rootViewController = navController
            self.window?.makeKeyAndVisible()


    }
        }