Andrea Toso Andrea Toso - 10 days ago 6
Swift Question

Change UserDefaults bool value when deleting app

I have a

bool value which tell me when I have to present my Walkthrough at the first installation of the app.When I install it, my bool value for key "firstAccessToApp" change from true to false, but if I remove the app from my device and I reinstall it, my walkthrough doesn't appear because the bool value is still false.How can I change the value when I remove my app from the device?This is my code:

if !defaults.bool(forKey: "firstAccessToApp") {
present(Walkthrough(), animated: true, completion: nil)
UserDefaults.standard.set(true, forKey: "firstAccessToApp")


When you install an app, the key doesn't exist in the UserDefaults and if you try to access it, it will return the default value, which is false. To set it to true when you first install and open the app, you can add this code to your func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool method:

let defaults = UserDefaults.standard

if (!defaults.dictionaryRepresentation().keys.contains("firstAccessToApp")) {
    defaults.set(true, forKey: "firstAccessToApp")