-4

I'm new to programming. I don't have IOS device yet. I tested my app on Xcode simulator. In general it was iPhone XR and sometimes on other models. When I first start the simulator with a different model, all the settings are off. I mean, switch is off slider value is set as 0 and etc.. I'd like to set them on and give the default value. How do I detect if my app starts first on a different device?

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
ssssw
  • 1
  • 5
  • You could use UserDefaults for this, read from it and if no values exist for your app then it is the first time – Joakim Danielson Jan 05 '20 at 13:07
  • As Joakim said use UserDefaults and [register](https://developer.apple.com/documentation/foundation/userdefaults/1417065-register) the key/value pairs with default values. – vadian Jan 05 '20 at 13:12
  • Does this answer your question? [iOS app first launch](https://stackoverflow.com/questions/26830285/ios-app-first-launch) – Cristik Jan 05 '20 at 15:13
  • @Cristik yes it does! thank you. – ssssw Jan 05 '20 at 15:30

1 Answers1

3

I would do something like this in the AppDelegate

let notFirstRun = UserDefaults.standard.bool(forKey: "notFirstLaunch")
if notFirstLaunch  {
    print("Not first launch.")
} else {
    print("First launch, setting UserDefault.")
    UserDefaults.standard.set(true, forKey: "notFirstLaunch")
}
Pablo Martinez
  • 1,573
  • 12
  • 31