[英]IOS: Code to present login screen modally
I want to password protect app with login screen (and join screen for first use). 我想用登录屏幕(和首次使用的加入屏幕)对应用程序进行密码保护。 Some answers on SO suggest testing if user is logged in in viewdidappear of initial screen and, if not logged in, presenting the login screen modally.
SO上的一些答案建议测试用户是否以初始屏幕的外观出现在viewdid中,如果没有登录则以模态方式显示登录屏幕。
I tried this but code is not working. 我试过了,但是代码不起作用。 Does anyone know up to date code for presenting a modal view controller?
有人知道用于呈现模态视图控制器的最新代码吗? Note I created login screen in storyboard and have given it storyboard id "login".
注意我在情节提要中创建了登录屏幕,并为其指定了情节提要ID“ login”。
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
//if not logged in, modally present login screen here.
if(![[NSUserDefaults standardUserDefaults] boolForKey:@"loggedIn"]) {
// go to login screen
NSLog(@"not logged in");//this fires so logic is ok
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *ivc = [storyboard instantiateViewControllerWithIdentifier:@"login"];//this line gives warning it is not being used
} else {
// go to main screen
}
}
/*perhaps I should call this somewhere?
- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated
*/
Credit to @rdelmar comment, You have to presentViewController:animated:completion: (presentModalViewController:animated:
that ivc. 感谢@rdelmar的评论,您必须具有
presentViewController:animated:completion: (presentModalViewController:animated:
该ivc。
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
//if not logged in, modally present login screen here.
if(![[NSUserDefaults standardUserDefaults] boolForKey:@"loggedIn"]) {
// go to login screen
NSLog(@"not logged in");//this fires so logic is ok
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *ivc = [storyboard instantiateViewControllerWithIdentifier:@"login"];//LOOK AT NEXT LINE
[self presentViewController:ivc animated:YES completion:nil]; //THIS LINE IS MISSING.
} else {
// go to main screen
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.