Detect when an iOS app is launched for the first t

2019-01-02 19:50发布

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

7条回答
还给你的自由
2楼-- · 2019-01-02 19:50

This will not work properly if you want to detect during other places of the code if its first launch. The "applicationWillTerminate" will not work from iOS 4.0 due to multitasking. this link provides a good solution: http://mobiledevblog.metalcompass.com/?p=43

查看更多
一个人的天荒地老
3楼-- · 2019-01-02 19:53

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:

// -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"];
查看更多
后来的你喜欢了谁
4楼-- · 2019-01-02 19:53

Save it as a user preference, eg had_first_launch, set to true on startup, it will only be false on the first time...

查看更多
宁负流年不负卿
5楼-- · 2019-01-02 19:59

You can set a boolean value in the user defaults to do this. Set the key to false when you call registerDefaults:, and then set it to true change it to true after you've shown your initial help screen or whatever you need to do.

If you have a persistent data file that's always saved after the app closes, checking to see if it exists would be another way.

查看更多
裙下三千臣
6楼-- · 2019-01-02 20:00

This is a really simple shortcut but I found that NSUserDefault key value pairs are always NULL the first time you run an app so

// Check to see if its the first time
if ([[NSUserDefaults standardUserDefaults] valueForKey:@"firstTime"] == NULL) {
   [[NSUserDefaults standardUserDefaults] setValue:@"Not" forKey:@"firstTime"];
}

and place this code in the awakeFromNib of the view controller that appears when your application launches. I don't know if any of the other answers work for your problem, but this is the way I solved it.

查看更多
只靠听说
7楼-- · 2019-01-02 20:07

I realize this question is quite old, but I used it to come up with one method of detecting the first startup after a "fresh install" (vs. first startup after an upgrade/downgrade) and thought I'd share the code here for future viewers in case it's helpful.

// Get current version ("Bundle Version") from the default Info.plist file
NSString *currentVersion = (NSString*)[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
NSArray *prevStartupVersions = [[NSUserDefaults standardUserDefaults] arrayForKey:@"prevStartupVersions"];
if (prevStartupVersions == nil) 
{
    // Starting up for first time with NO pre-existing installs (e.g., fresh 
    // install of some version)
    [self firstStartAfterFreshInstall];
    [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObject:currentVersion] forKey:@"prevStartupVersions"];
}
else
{
    if (![prevStartupVersions containsObject:currentVersion]) 
    {
        // Starting up for first time with this version of the app. This
        // means a different version of the app was alread installed once 
        // and started.
        [self firstStartAfterUpgradeDowngrade];
        NSMutableArray *updatedPrevStartVersions = [NSMutableArray arrayWithArray:prevStartupVersions];
        [updatedPrevStartVersions addObject:currentVersion];
        [[NSUserDefaults standardUserDefaults] setObject:updatedPrevStartVersions forKey:@"prevStartupVersions"];
    }
}

// Save changes to disk
[[NSUserDefaults standardUserDefaults] synchronize];
查看更多
登录 后发表回答