Know when the App is launched for the first time in IOS

3

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.

    
asked by Comunidad 30.08.2016 в 13:28
source

3 answers

4

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")
}
    
answered by 30.08.2016 / 13:51
source
2

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"];
    
answered by 12.01.2017 в 14:01
0

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") 
    
answered by 26.04.2018 в 16:26