Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Brand new to this site, pretty amateur knowledge here! Started teaching myself a few weeks ago. Got a pretty solid iPhone app however the last feature I would like to implement is the ability to;

Create a 'first launch only' guided tour.

What i want to know is; if it is the users first launch of the application how can i redirect the view to a new view controller that isn't the 'initial view controller' without the tap of a button, all programatically.

Ive read a few tutorials about detecting first launch which i understand. Ive also read a few tutorials and tried everything in the book to try and implement "performSegueWithIdentifier" however nothing is working for me!

Perhaps its because I'm using Xcode 5 and testing on iOS 7. If anyone can help me, I would be forever grateful!

(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.
}
like image 258
Luke Avatar asked Oct 15 '13 08:10

Luke


1 Answers

If you have not registered any defaults using [[NSUserDefaults standardDefaults] registerDefaults:], the first time you call [[NSUserDefaults standardDefaults] boolForKey:@"FirstLaunch"] you will receive NO as that key does not exist.

I prefer to use a more semantic key name, such as hasPerformedFirstLaunch, then it's a matter of checking if that returns NO and executing the first launch sequence:

- (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.
}
like image 101
neilco Avatar answered Dec 19 '22 15:12

neilco