繁体   English   中英

iOS首次发布之旅 - 检测应用是否首次启动

[英]iOS first launch tour - detecting if the app is launched for the first time

这个网站全新,这里有很多业余知识! 几个星期前开始自学。 有一个非常可靠的iPhone应用程序,但我想要实现的最后一个功能是能够;

创建“首次启动”导游。

我想知道的是; 如果是用户首次启动应用程序,我怎么能以编程方式将视图重定向到不是“初始视图控制器”的新视图控制器,而无需点击按钮。

我已经阅读了一些有关检测首次启动的教程,我理解。 我也阅读了一些教程并尝试了书中的所有内容来尝试实现“performSegueWithIdentifier”,但是没有什么对我有用!

也许是因为我正在使用Xcode 5并在iOS 7上进行测试。如果有人能帮助我,我将永远感激不尽!

(void)viewDidLoad
{
    [super viewDidLoad];

    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"FirstLaunch"]) {
    }
    else {
        // Place code here
        self.view.backgroundColor = [UIColor redColor];

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

    // Do any additional setup after loading the view, typically from a nib.
}

如果您没有使用[[NSUserDefaults standardDefaults] registerDefaults:]注册任何默认值,则第一次调用[[NSUserDefaults standardDefaults] boolForKey:@"FirstLaunch"]您将收到NO因为该键不存在。

我更喜欢使用更多的语义键名,例如hasPerformedFirstLaunch ,然后检查是否返回NO并执行第一个启动序列:

- (void)viewDidLoad
{
    [super viewDidLoad];

    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"hasPerformedFirstLaunch"]) {
        // On first launch, this block will execute

        // Set the "hasPerformedFirstLaunch" key so this block won't execute again
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasPerformedFirstLaunch"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    else {
        // On subsequent launches, this block will execute
    }    

    // Do any additional setup after loading the view, typically from a nib.
}

这是一个导游来源https://github.com/sofienazzouz/Guided-Tour ,现在如果你想从你的代表那里打电话给导游,你应该这样称呼它

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

//// initialize your initialViewController here however you want
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"knewOrRecentUser"]) {
        self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.initialViewController];
   } else {
       [[NSUserDefaults standardUserDefaults] setObject:@"old" forKey:@"knewOrRecentUser"];
       GuidedTourViewController *guideTour = [[GuidedTourViewController alloc] init];
       self.navigationController = [[UINavigationController alloc] initWithRootViewController:guideTour];
   }
[self.window setRootViewController:self.navigationController];
}

neilco

您提供的内容确实有效,但您在第一次启动APP时遗漏了将屏幕变为红色(或您喜欢的颜色)的代码。

现在看起来似乎并不多,但是当一个新手冒险尝试时,最好提供尽可能多的信息,这样即使我们能够理解正在发生的事情。 是的,包括我!! << =新手

  - (void)viewDidLoad
{
    [super viewDidLoad];

    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"hasPerformedFirstLaunch"]) {
        // On first launch, this block will execute
        // Place code here
        self.view.backgroundColor = [UIColor redColor];

        // Set the "hasPerformedFirstLaunch" key so this block won't execute again
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasPerformedFirstLaunch"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    else {
        // On subsequent launches, this block will execute
    }

    // Do any additional setup after loading the view, typically from a nib.
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM