Check for first launch of my application

蓝咒 提交于 2019-12-12 08:51:14

问题


How would I check if it is the first launch of of my application using NSUserDefaults and running some code for the first time my app opens?


回答1:


This should point you in 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];
}



回答2:


The NSUserDefaults answer is the first thing that popped in my head, but upon reflection I will make another suggestion. A bit more work, but it's worth considering. The motive is: sometimes when troubleshooting an app, Apple recommends deleting that app's plist file. It's a fairly ubiquitous troubleshooting technique. I would recommend storing your boolean in your plist file instead of NSUserDefaults.

Disclaimer: I only do iOS development, so I'm not sure how NSUserDefaults and plists interact on the Mac, and I don't know what all is involved in getting your plist to live in ~/Library/Application\ Support/Preferences/com.mycompany.MyAppName.plist

Anyway, I imagine what this requires is having some code which can actually author a "fresh" plist (probably a copy from a template file in your bundle), and you app does this if it launches and does not see a plist. The default plist should not include the flag which lets your users skip the 'first time' code, but if they have opened the app before, and then delete the plist, they should get default behavior back.

This is an important behavior to support where possible, to aide our users if our app ever gives them trouble.




回答3:


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 into the program and have them available whenever you open it. You can use 'boolForKey' to set a flag called "hasBeenLaunched". By default, this value will be NO when not set. Once you change it to YES, the code in the if condition will never be executed again.




回答4:


In your main controller class, implement something like this:

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 +initialize class method is called before an instance of the class it's found in is created; in other words, it is called very early on, and is a good place to set up your default values.

See Preferences and Settings Programming Guide: Registering Your App's Default Preferences for more info.



来源:https://stackoverflow.com/questions/11371332/check-for-first-launch-of-my-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!