0
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
    {
        // app already launched
    }
    else
    {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        // This is the first launch ever
    }
    return YES;
}

I searched this question and got so many answers in Objective-C. Can anyone help me to do that in swift? Thank you in advance.

Sujay
  • 2,510
  • 2
  • 27
  • 47
erjin
  • 3
  • 1
  • 5

1 Answers1

12

Try this one

if(NSUserDefaults.standardUserDefaults().boolForKey("HasLaunchedOnce") {
  // App already launched

} else {
  // This is the first launch ever
  NSUserDefaults.standardUserDefaults().setBool(true, forKey: "HasLaunchedOnce")
  NSUserDefaults.standardUserDefaults().synchronize()
}

Swift 3 version->

if (UserDefaults.standard.bool(forKey: "HasLaunchedOnce")) {
   // App already launched

} else {
   // This is the first launch ever         
   UserDefaults.standard.set(true, forKey: "HasLaunchedOnce")
   UserDefaults.standard.synchronize()
}  
Daniel F
  • 13,620
  • 2
  • 29
  • 55
Ravi Gautam
  • 960
  • 2
  • 9
  • 20