-3

Is it possible to detect if it is the first time a user opens an iOS application, using Objective-C?

I would like to show a welcome message when the user opens up the app for the first time, but not show it to them after that.

I'm looking for something like:

BOOL firstTime = [AppDelegate isFirstTimeOpeningApplication]
Jojodmo
  • 23,357
  • 13
  • 65
  • 107
  • 3
    what have you tried so far? You mention you tried "the other ones" on stack overflow but not what methods you tried – Dan F Sep 11 '13 at 14:48
  • *they don't work if you shut off your phone* -- that doesn't make any sense. The only way to preserve any data between launches of the app is to write it to persistent storage; if it's written to persistent storage, it'll be there whether or not you cycle the power on your device. – Caleb Sep 11 '13 at 14:55

5 Answers5

3

Look for some value in your app's preferences, or the existence of some file. If you don't find it, your app is running for the first time, so write the expected value to the preferences or create the file so that the next time your app runs you'll know that it's not the first time.

If you store the time and date of the first run instead of just a flag, you can determine how long it's been since the app was used. You might want your app to act like it's the first run if the user hasn't used your app in a very long time.

Note that this technique only works if the user hasn't deleted your app. When an app is deleted, all its data is removed. If you want to know if your app has ever run on that device before, even if it was deleted afterward, you'll need to record information to identify the device elsewhere, such as on your own server.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • 1
    Items put in the keychain are not deleted with your app, so this can be a useful way to store small pieces of information between uninstalls. – Rob Napier Sep 11 '13 at 21:54
  • @RobNapier I thought that might be the case, but wasn't sure enough to suggest it and too short on time to look it up. Thanks for adding that -- it's a lot easier to use the keychain than to set up a server just for that purpose. – Caleb Sep 11 '13 at 22:23
1

You can save a "I've run before" flag in NSUserDefaults.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
1

Use NSUserDefaults

//In applicationDidFinishLaunching:withOptions:
BOOL hasLaunchedBefore = [[NSUserDefaults standardUserDefaults] boolForKey:@"AppHasLaunchedBefore"];
if(!hasLaunchedBefore) {
    //First launch
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"AppHasLaunchedBefore"];
}
else {
    //Not first launch
}

Hope that helps!

Amar
  • 13,202
  • 7
  • 53
  • 71
0
- (void)applicationDidFinishLaunching:(UIApplication *)application {
 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    NSString *firsttime = [defaults stringForKey:@"firsttime"];
    if (firsttime == nil) {

        TheOtherViewController *Other = [[TheOtherViewController alloc] initWithNibName:nil bundle:nil];
        Other.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [window addSubview: Other.view];        

        [defaults setObject:@"lasttime" forKey:@"firsttime"];
    }   else {  [window addSubview:viewController.view];
        [window makeKeyAndVisible];
    }
}
Baby Groot
  • 4,637
  • 39
  • 52
  • 71
0

Use NSUserDefaults to store a flag which will indicate if the user has launched the app before or not. If the value of flag .. lets say "IsFirstTimeLaunched" is false then it means the user has not launched the app before.

azamsharp
  • 19,710
  • 36
  • 144
  • 222