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.

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

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?

This answer solved this issue for us.

UserDefaults on App Launch
 
 
Q