Last active
July 10, 2022 15:31
-
-
Save akingdom/5bfdaeee6dfe6540a2009d6489aeec9f to your computer and use it in GitHub Desktop.
Code to detect whether this is an initial app launch or subsequent.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Objective C++ | |
// Detects whether this is an initial or subsequent launch of this app. | |
// | |
// By Andrew Kingdom | |
// MIT license | |
// | |
static BOOL _appDidLaunchPreviously; | |
static BOOL _appDidLaunchPreviously_known; | |
static NSString * kAppDidLaunchPreviously = @"priorLaunch"; | |
static NSString * vAppDidLaunchPreviously = @"Y"; | |
/// Put this in the AppDelegate | |
/// Usage: BOOL initialLaunch = ! AppDelegate.appDidLaunchPreviously; | |
/// Returns FALSE if this is the app's first launch. | |
/// This changes the second time the app re-launches, not the second time this is called. | |
/// (Not thread safe as-is) | |
+ (BOOL)appDidLaunchPreviously { // getter | |
if(_appDidLaunchPreviously_known == TRUE) { | |
// We already know the value, so return it | |
return _appDidLaunchPreviously; | |
} | |
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; | |
// Get the value if defined | |
id value = [defaults valueForKey:kAppDidLaunchPreviously]; | |
if(value == nil) { | |
// If not defined, set the value (once per install) -- we'll read it on next launch | |
[defaults setValue:vAppDidLaunchPreviously forKey:kAppDidLaunchPreviously]; | |
} | |
_appDidLaunchPreviously_known = TRUE; | |
_appDidLaunchPreviously = [value isEqualToString:vAppDidLaunchPreviously]; | |
return _appDidLaunchPreviously; // First launch | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment