如何设置首次启动iOS应用程序

我只想在我的应用程序首次启动时显示条款和条件。

目前我正在写我的代码在didFinishLaunchingWithOptions

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Handle launching from a notification if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"]) { NSLog(@"Already Run"); } else { [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"]; [[NSUserDefaults standardUserDefaults] synchronize]; UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil]; FirstPage * termsView = [storyboard instantiateViewControllerWithIdentifier:@"FirstPage"]; UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:termsView]; self.window.rootViewController=navController; // This is the first launch ever } } 

更新::

当我的应用程序在TERMSandCONDITIONSVC第一次启动时,我想要什么我有一个Acceptbutton。 当用户Accepts聚集时,只有他允许进一步。

以上代码的问题:

当我运行我的应用程序第一次TermsandCondition显示给我。我按回家的button应用程序在Background.Again我打开从启动程序的应用程序和TermsandCondtion显示。

但是,当我把我的应用程序在背景中,并通过双击主页button从背景中删除它,并运行应用程序,我直接转移到我的主屏幕。

没有条款和条件。

我如何从这恢复。

由于没有将viewcontroller设置为根视图,因此您会看到黑屏。

做一件事情在IF部分写下你的其他部分,并将你的控制器从术语页面改为第二页面。

尝试在didFinishLaunch中input以下代码并进行检查。

 self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds]; UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; UINavigationController *navController if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"]) { NSLog(@"Already Run"); SecondView *secondView = [storyboard instantiateViewControllerWithIdentifier:<storyboard id>]; navController=[[UINavigationController alloc]initWithRootViewController:secondView]; } else { [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"]; [[NSUserDefaults standardUserDefaults] synchronize]; TermsView *termsView = [storyboard instantiateViewControllerWithIdentifier:<storyboard id>]; navController=[[UINavigationController alloc]initWithRootViewController:termsView]; // This is the first launch ever } } self.window.rootViewController=navController; return YES; 

更新:只需拖动下面的行以同意button的单击事件,并从didFinishlaunching删除此行。

 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"]; [[NSUserDefaults standardUserDefaults] synchronize]; 

您正在以错误的方式实例化您的termsView 。 在故事板中,为FirstPage分配FirstPage视图控制器的storyboardID。

然后在您的didFinishLaunchingWithOptions执行以下操作

 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil]; FirstPage * termsView = [storyboard instantiateViewControllerWithIdentifier:@"FirstPage"]; 

这将find你的主要故事板,然后基于提供的storyboardID(即FirstPage )与ViewController实例化termsView

如果你想设置第一次启动的iOS应用程序。 使用此代码的条款和条件将出现或显示只有第一次的应用程序启动。

  if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"]) { NSLog(@"Already Run"); } else { [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"]; [[NSUserDefaults standardUserDefaults] synchronize]; FirstPage *termsView = [[FirstPage alloc]init]; UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:termsView]; self.window.rootViewController=navController; // This is the first launch ever UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:nil]; //first goto storyboard and give storyboard id "TermsConditionVC" to TermsConditionVC TermsConditionVC *firstVC = [story instantiateViewControllerWithIdentifier:@"TermsConditionVC"]; self.window.rootViewController =firstVC; } 

为完整的文章和完整的过程来设置您的第一个计时器iOS应用程序启动访问下面的链
http://findnerd.com/list/view/How-to-setup-first-time-launch-of-an-iOS-App/2770/