This question is in Android
, but how would it be in iOS
?
How can you tell if it's the first time the app starts?
It would be interesting, in the case that the user updates the application is detected as new or update.
In objective C:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
return YES;
}
In swift using NSUserDefault:
let launchedBefore = NSUserDefaults.standardUserDefaults().boolForKey("launchedBefore")
if launchedBefore {
print("anteriormente iniciada.")
}
else {
print("Se inicia por primera vez!..")
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "launchedBefore")
}
Completing the answer, you first fill in the variable
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (![[NSUserDefaults standardUserDefaults] introducir el código aquíboolForKey:@"HasLaunchedOnce"])
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
return YES;
}
Then read the variable if you need it somewhere
NSString *value = [[NSUserDefaults standardUserDefaults] stringForKey:@"HasLaunchedOnce"];
I add the code for Swift 4
Save data:
UserDefaults.standard.set(<value>, forKey: "key_name")
UserDefaults.standard.synchronize() //Optional
Retrieve data
var returnValue: [datatype]? = UserDefaults.standard.object(forKey: "key_name") as? [datatype]
And I also add delete them , which in the previous answers is not:
UserDefaults.standard.removeObject(forKey:"key_name")