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

时间:2013-10-15 08:11:07

标签: ios xcode segue viewcontroller

这个网站的全新,非常业余知识在这里!几个星期前开始自学。有一个非常可靠的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.
}

3 个答案:

答案 0 :(得分:6)

如果您未使用[[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.
}

答案 1 :(得分:1)

这是一个导游资料来源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];
}

答案 2 :(得分:1)

<强> neilco

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

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

  - (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.
}