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

3

5

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.
}
Tamathatamaulipas answered 15/10, 2013 at 8:11 Comment(4)
What have you tried? Maybe show us some code. How is what you've tried not working? More the detail you can give, the better equipped we'll be to help you.Bohon
I added to my main post what I added to my viewcontroller.m file. That works fine however I'm not quite sure what to put the in the "//Place code here" part to open an alternative view controller.Tamathatamaulipas
I have also attempted to create a segue and push it programatically using code by placing this code where it says "//place code here" above; [self performSegueWithIdentifier:@"Tour" sender:self]; My segue being named "Tour"Tamathatamaulipas
Have you called [[NSUserDefaults standardDefaults] registerDefaults:] anywhere in your code?Bohon
B
6

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.
}
Bohon answered 15/10, 2013 at 10:29 Comment(7)
Being relatively new to code, that helps so much, being able to distinguish one from ther simply is a huge help. So now that We've established how to recognise a first launch and save that information, how do programmatically perform a segue to another view controller? I've attempted using 'performseguewithidentifier' but nothing happens!Tamathatamaulipas
I've not tried it as I don't use storyboards much, but you'll probably have better luck doing the first launch check and performing the segue in viewWillAppear:. performSegueWithIdentifier: should work.Bohon
I've tried using in in viewWillAppear and viewDidLoad but nothing happens. It doesn't trigger anything!Tamathatamaulipas
Also I've double checked and made sure that my segue's name is copied directly into the code so it's linking to the right one. I created my segue ctrl-click dragging from the view controller icon onto the tour view controller.Tamathatamaulipas
Thanks for your help so far but I'm still stuck!Tamathatamaulipas
It works fine now with the code you posted above however in the debug panel i am getting this message; 2013-10-20 18:14:23.549 Rawr![66306:a0b] Unbalanced calls to begin/end appearance transitions for <UINavigationController: 0xa45a700>. Should that be anything to worry about?Tamathatamaulipas
Delay the segue by a couple of hundred milliseconds using dispatch_after so that any pending operations in viewDidLoad have time to complete.Bohon
M
1

Here is a Guided tour sources https://github.com/sofienazzouz/Guided-Tour , now if you want to call the guided tour from your delegate you should call it like this

- (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];
}
Mucoviscidosis answered 30/12, 2013 at 16:22 Comment(0)
B
1

neilco

What you provided does work but you left out the code to turn the screen red (or what ever color you like) during the first launch of the APP.

Now this may not seem like much but when a newbie does venture through it is best to provide as much info as possible so that even WE can understand what is happening. YES that does include ME!! <<= 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.
}
Brandabrandais answered 29/1, 2014 at 11:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.