如何在应用启动时首次显示登录屏幕

时间:2013-07-03 09:10:53

标签: ios ipad launching-application

我希望在第一次启动其他明智的应用程序工作时启动登录屏幕,但问题是再次进入登录屏幕。

这是我在didFininsh中使用的代码

我希望用户show首次登录登录屏幕,下次应该显示splitViewController

     [[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],@"firstLaunch",nil]];

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) {



    [self.window addSubview:[splitViewController view]];

    LoginViewController *targetController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
    targetController.modalPresentationStyle = UIModalPresentationFullScreen;


    [self.splitViewController presentViewController:targetController animated:YES completion:nil];

    }

else {


    [self.window addSubview:[splitViewController view]];

}


// my comment[window addSubview:splitViewController.view];
[window makeKeyAndVisible];


return YES;

4 个答案:

答案 0 :(得分:3)

如果您只想显示一次登录屏幕,那么您需要帮助数据库。

我给出了如何使用数据库表的简单示例。

假设您的表名是“登录”

在4个字段中添加登录

  

id - auto-inc。
  用户名 - TEXT;
  密码 - TEXT;
  status - TEXT,默认为0;

拿两个ViewController

1)LoginViewController
2)HomeViewController

并在application:didFinishLaunchingWithOptions

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

     /// here you need to get status from "Login" table 

     if (status == 0)
     {
        /// code of initialize LoginViewController
       self.window.rootViewController = self.LoginViewController;
     }
     else if (status == 1)
     {
          /// code of initialize HomeViewController
       self.window.rootViewController = self.HomeViewController;     
     }
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

在上面的代码中,当你启动app时。当时第一次从登录表中查看状态的条件。

你第一次得到 status = 0 因为我们把状态的默认值设为 0 所以这里,

self.window.rootViewController = self.LoginViewController;

此时您在登录屏幕上输入userName和Password。 在输入用户名和密码后,您点击登录按钮,此时您还需要点击查询更新状态 '登录' 1

当你再次为应用程序加注星标时,你状态为1 所以,

self.window.rootViewController = self.HomeViewController;     

因此,您无法再次显示登录屏幕。

答案 1 :(得分:0)

if (![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) {



[self.window addSubview:[splitViewController view]];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
LoginViewController *targetController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
targetController.modalPresentationStyle = UIModalPresentationFullScreen;


[self.splitViewController presentViewController:targetController animated:YES completion:nil];

}

else {


[self.window addSubview:[splitViewController view]];

}


// my comment[window addSubview:splitViewController.view];
[window makeKeyAndVisible];


return YES;

答案 2 :(得分:0)

您应该在Controller中实现需要跳过的viewDidAppear方法,然后在viewDidAppear方法中,对要发送给用户的视图执行segue,如下所示:

- (void)viewDidAppear:(BOOL)animated {

  //if user is already logged in then skip to welcome view
  //implement your login method here

  if ([self isUserLoggedIn]) {
    [self performSegueWithIdentifier:@"WelcomeTo" sender:self];
  }

}

for ios7

答案 3 :(得分:-1)

试试这个......

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) {

[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstLaunch"];

    [self.window addSubview:[splitViewController view]];

    LoginViewController *targetController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
    targetController.modalPresentationStyle = UIModalPresentationFullScreen;


    [self.splitViewController presentViewController:targetController animated:YES completion:nil];

    }

else {


    [self.window addSubview:[splitViewController view]];

}