UserDefaults on App Launch

Hi

On first launch (after download) of my app I display a "terms and conditions" consent screen. When the user taps agree, I set a Bool value in UserDefaults, so on next launch, the user is not prompted again. Pretty stock standard approach.

I've had some users report that if the app is in the background for an extended period of time, the "terms and conditions" screen will re-appear when brought back into the foreground.

But if the app was terminated after use and then re-launched, then behaviour is as expected - the "terms and conditions" screen is not shown.

Is the better approach to use a file instead?

Thanks

  • Can you show your code checking UserDefaults including enough context to guess what may be happening?

  • @OOPer I added some context for you.

Add a Comment

Replies

Hi

I have a LaunchViewController which is set as the initial view controller on the storyboard. The LaunchViewController checks a key in the UserDefaults then either navigates to the user licence or the welcome screen.

class LaunchViewController: UIViewController {
   override func viewDidLoad() {
      super.viewDidLoad()
        
      var viewControllerIdentifier = "Welcome"
      let storyboard = UIStoryboard(name: "Main", bundle: nil)

      // Check if the user agreement has been accepted.
      if !UserDefaults.standard.bool(forKey: "HasAchnowledgedUserLicence") {
        viewControllerIdentifier = "UserLicence"
      }
      
      let viewController = storyboard.instantiateViewController(identifier: viewControllerIdentifier)
      navigationController?.pushViewController(viewController, animated: true)
    }
}

As the user progresses through the app and needs to go back the Welcome screen, I execute this code:

_ = navigationController?.popToRootViewController(animated: false)`

If you think I should be setting the root view controller instead of performing a navigation as in:

window?.rootViewController = viewConntoller
window?.makeKeyAndVisible()

Many thanks for helping

  • Thanks for showing. So how or when or where do you set "HasAchnowledgedUserLicence"?

  • On first launch, HasAchnowledgedUserLicence returns false as there is no key for it in UserDefaults and navigates to the UserLicenceViewController where some text is displayed for the user to agree to. The user tap "Agree" which sets HasAchnowledgedUserLicence to true then _ = navigationController?.popToRootViewController(animated: false) is invoked, where the condition is now met and the WelcomeViewController is shown.

  • Thanks for trying, but far from enough to guess what's happening for me. Hope you can solve your issue soon.

Add a Comment

One of our apps is facing the same issue. We've seen a huge spike in the behavior you've described after the iOS 15 launch. Before that, only a small subset of users had problems with that.

Sadly, we were unable to find the root cause of the issue. Our temporary solution was to completely replace UserDefaults with our own implementation.

Did you manage to solve this issue @craigaps?

  • I'm also experiencing this same issue using UserDefaults with iOS 15. It only happens to some users and it's not easily reproducible. I'm not sure what to do because my original implementation involved writing a custom file in plist format that also stopped working reliably with iOS 15.

Add a Comment

This answer solved this issue for us.