如何设置首次启动 iOS 应用程序 [英] How to setup first time launch of an iOS App

查看:48
本文介绍了如何设置首次启动 iOS 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to show the terms and conditions only the first time of my app launch.

目前我正在didFinishLaunchingWithOptions中编写我的代码.

Currently I am writing my code in 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 中启动时我想要什么 我有一个 Accept 按钮.当用户 Accepts 同意时,只有他允许走得更远.

What I want when my application launched first time in TERMSandCONDITIONSVC I have an Accept Button. When User Accepts the aggrement then only He allowed go further.

上述代码的问题:

当我第一次运行我的应用程序时 TermsandCondition 显示给我.我按下主页按钮,应用程序进入后台.再次我从启动器打开应用程序,TermsandCondtion 显示.

When i run my app first time TermsandCondition displayed to me.I press home button the app goes in Background.Again I open The app from launcher and TermsandCondtion showed.

但是当我将我的应用程序置于后台并通过双击主页按钮将其从后台删除并运行该应用程序时,我会直接转移到我的主屏幕.

But When I put my app in Background and By adouble tapping Home button remove it from background and run the app I am directly transfered to my home screen.

无条款和条件.

我该如何从中恢复.

推荐答案

由于没有将 viewcontroller 设置为根视图,您正在黑屏.

You are getting black screen because no viewcontroller is set as root view.

做一件事,将您的 else 部分也写入 IF 部分,并将您的控制器从条款页面更改为第二页.

Do one thing Write your else part in IF part also and change your controller from terms page to second page.

尝试将以下代码放入 didFinishLaunch 并检查.

Try to put below code in didFinishLaunch and check.

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;

更新:只需拖动下方几行即可同意按钮的点击事件,并从 didFinishlaunching 中删除此行.

UPDATE: just drag below lines to agree button's click event and remove this lines from didFinishlaunching.

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

这篇关于如何设置首次启动 iOS 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆