Search code examples
ioscocoa-touchlaunching-application

Detect when an iOS app is launched for the first time?


How do I detect when an iOS app is launched for the first time?


Solution

  • Pretty much what Marc and Chris said, though I prefer to change the value when the app quits in case there're multiple areas of the application that need to know about it. In code:

    Objective-C

    // -applicationDidFinishLaunching:
    [[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],@"firstLaunch",nil]];
    // to check it:
    [[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"];
    // -applicationWillTerminate:
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstLaunch"];
    

    Swift 5.0

    // -applicationDidFinishLaunching:
    UserDefaults.standard.register(defaults: ["firstLaunch":true])
    // to check it:
    UserDefaults.standard.bool(forKey: "firstLaunch")
    // -applicationWillTerminate:
    UserDefaults.standard.set(false, forKey: "firstLaunch")