Check out the first launch of my application

How can I check if this is the first launch of my application using NSUserDefaults and the execution of some code the first time I open my application?

+6
source share
4 answers

This should indicate the right direction:

 static NSString* const hasRunAppOnceKey = @"hasRunAppOnceKey"; NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; if ([defaults boolForKey:hasRunAppOnceKey] == NO) { // Some code you want to run on first use... [defaults setBool:YES forKey:hasRunAppOnceKey]; } 
+19
source

NSUserDefaults answer is the first thing that appeared in my head, but after thinking it over, I will make another suggestion. A bit more work, but it's worth considering. The motive is that sometimes when troubleshooting, the Apple application recommends deleting this application plist file. This is a fairly common troubleshooting method. I would recommend storing your boolean in your plist file instead of NSUserDefaults.

Disclaimer: I am only developing iOS, so I’m not sure how NSUserDefaults and plists interact on Mac, and I don’t know that everything is involved in the fact that your plist lives in ~ / Library / Application \ Support / Preferences / com .mycompany.MyAppName.plist

In any case, I assume that this requires some code that can actually create a "fresh" plist (perhaps a copy from the template file in your kit), and you will do this if it starts and does not see plist. The default plate should not include a flag that allows your users to skip the code "first time", but if they opened the application earlier and then removed plist, they should return the default behavior.

This is an important behavior to support, where possible, to support our users if our application ever creates problems.

+2
source
 if (![[NSUserDefaults standardUserDefaults] boolForKey:@"hasBeenLaunched"]) { // Run code on the first launch only ... [defaults setBool:YES forKey:@"hasBeenLaunched"]; } 

You can use NSUserDefaults to save bools, integers, objects in the program and their availability each time it is opened. You can use "boolForKey" to set a flag named "hasBeenLaunched". By default, this value will be NO if not set. Once you change it to YES, the code in the if condition will never be executed again.

+2
source

In your main controller class, do the following:

 static NSString * const MDFirstRunKey = @"MDFirstRun"; @implementation MDAppController + (void)initialize { NSMutableDictionary *defaults = [NSMutableDictionary dictionary]; [defaults setObject:[NSNumber numberWithBool:YES] forKey:MDFirstRunKey]; [[NSUserDefaults standardUserDefaults] registerDefaults:defaults]; // the following if on Mac and is necessary: [[NSUserDefaultsController sharedUserDefaultsController] setInitialValues:defaults]; } - (void)applicationDidFinishLaunching:(NSNotification *)notification { BOOL firstRun = [[[NSUserDefaults standardUserDefaults] objectForKey:MDFirstRunKey] boolValue]; if (firstRun) { // do something [[NSUserDefaults standardUserDefaults] setObject: [NSNumber numberWithBool:NO] forKey:MDFirstRunKey]; } else { // do something else } } @end 

The class method +initialize is called before creating an instance of the class that it found; In other words, this is called very early, and it is a good place to set your default values.

For more information, see Settings and Preferences. Programming Guide: Registering default settings in the application .

+1
source

Source: https://habr.com/ru/post/919916/


All Articles