我有一个带有按钮的主屏幕,当我们点击按钮显示登录屏幕时,用户通过电话号码登录,如果用户输入未注册的号码,则在屏幕上显示按钮前往注册表单屏幕,现在我正在尝试,如果用户已经登录,那么无论何时打开应用程序都不应该看到登录屏幕,它应该直接转到登录后出现的下一个屏幕,如果用户之前没有登录它应该显示登录屏幕。我的代码是,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
if([[NSUserDefaults standardUserDefaults] valueForKey:@"login"])
{
// So, here user already login then set your root view controller, let's say `SecondViewController``
HomeViewController *secondViewController = [storyBoard instantiateViewControllerWithIdentifier:@"HomeViewController"];
// then set your root view controller
self.window.rootViewController = secondViewController;
} else
{
// It means you need to your root view controller is your login view controller, so let's create it
RegisterViewController *loginViewController= [storyBoard instantiateViewControllerWithIdentifier:@"RegisterD"];
self.window.rootViewController = loginViewController;
}
// Override point for customization after application launch.
return YES;
}
答案 0 :(得分:1)
将密钥的boolforkey insted值用作
[[NSUserDefaults standardUserDefaults] boolForKey:@"login"]
答案 1 :(得分:0)
您可以使用NSUserDefault设置当前用户登录状态。 在下一个应用程序启动时,在主视图控制器上,当您单击该按钮时,在加载登录屏幕之前,请检查用户登录状态。如果是,则直接加载下一个屏幕,如果没有重定向到登录屏幕。
func loginButtonOnHomeScreen(sender:UIButton){
let loginState = UserDefaults.standard.bool(forKey: "Login")
var VC:UIViewController
if loginState == true{
VC = (self.storyboard?.instantiateViewController(withIdentifier: "NextScreen"))!
}else{
VC = (self.storyboard?.instantiateViewController(withIdentifier: "LoginScreen"))!
}
self.navigationController?.pushViewController(VC, animated: true)
}
希望这会有所帮助
编辑 n目标C
BOOL loginState = [[NSUserDefaults standardUserDefaults] boolForKey:@"Login"];
UIViewController *VC;
if (loginState == true){
VC = [self.storyboard instantiateViewControllerWithIdentifier:@"NextScreen"];
}else{
VC = [self.storyboard instantiateViewControllerWithIdentifier:@"LoginScreen"];
}
[self.navigationController pushViewController:VC animated:true];
答案 2 :(得分:0)
用户登录/注册成功设置
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"login"];
当用户注销时从NSUserDefaults中删除或设置false
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"login"];
OR
[[NSUserDefaults standardUserDefaults] setBool:FALSE forKey:@"login"];
您可以在appDelegate文件或任何其他合适的屏幕中检查用户是否已登录
if([[NSUserDefaults standardUserDefaults] boolForKey:@"login"])
{
//YES Already Login
NextController *nextVC = [self.storyboard instantiateViewControllerWithIdentifier:@"NextController"];
}
else
{
//NOT Login
LoginController *loginVC = [self.storyboard instantiateViewControllerWithIdentifier:@"LoginController"];
}
答案 3 :(得分:0)
你有2个ViewController(LoginVC和HomeVC), 首先在" LoginVC"
中编写波纹管代码if ([[NSUserDefaults standardUserDefaults] boolForKey:@"loggedIn"]){
nslog(@"Success"};
}
else{
nslog(@"Login Agin")
}
HomeVC中的第二个代码写入(此登录后)
-(void)viewWillAppear:(BOOL)animated{
[[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"loggedIn"];
}