Do NSUserDefaults persist through an Update to an app in the Appstore?

IphoneCrashNsuserdefaults

Iphone Problem Overview


Is this the case? Do NSUserDefaults get reset when you submit an update to an app on the App Store, or are they reset?

My app is crashing when updated but not crashing when downloaded fully - so I'm trying to determine what could possibly be different in the updated session to the freshly downloaded session.

Cheers, Nick.

Iphone Solutions


Solution 1 - Iphone

They are usually not reset unless the user deletes the app. For basic data, NSUserDefaults is the best way to save data such as preferences, dates, strings etc. If you are looking to save images and files, the file system is a better bet.

Solution 2 - Iphone

I beleive the answer is YES, it will persist. This also fully documented under the Application Directory chapter in the Apple iPhone OS Programming Guide.

Solution 3 - Iphone

  1. Direct answer to the posted question: YES.
  2. Your problem: Your app gets crashed due to logic issues. Suppose you store an object in defaults and the app checks it's value on launch (or elsewhere). In you update you could change the way it is checked or used, e.g. you expect a value, but the object is nil, or vice versa. This may cause a SIGABRT or EXC_BAD_ACCESS.

Solution 4 - Iphone

If you had CoreData model and you changed something in your model and update, without managing migration, thats probably reason why your app crashes on update...

Solution 5 - Iphone

I have a similar experience. Our app stores a version number in Settings.Bundle/Root.Plist. This gets displayed through the iPhone Settings app. What we find is that on an Install the version number gets loaded from the app bundle - therefore the version number is correct. On an update however the version number doesn't change. This gives the impression the user is running a previous version of the app. We don't have any logic linked to the version number, it's just for display (it could be used by contact centre staff when diagnosing faults).

Our experience is NSUserDefaults doesn't get cleared when a user updates our app, but the Settings display doesn't get updated either.

Solution 6 - Iphone

Be aware of this case, when your app is running in background and you cannot access your stored values in NSUserDefaults:

Eric:

> There have been many threads and bugs about this, but it's happening to me again in ios 9. I have an app that launches in the background in response to NSURLSession tasks and content-available pushes. Reproducibly, if I reboot my phone and wait for a background launch of my app to happen, then when I open the app I find that [[NSUserDefaults standardUserDefaults] dictionaryRepresentation] contains all the system values, e.g. AppleITunesStoreItemKinds, etc. but does not contain any of the values I have set. If I force-quit and relaunch the app all of my values come back. Is there any way to avoid it caching the "empty" standardUserDefaults from before the phone is unlocked, or at least to determine when they are messed up and fix them without having to force-quit the app?

Eskimo ([email protected]): > The problem here is that NSUserDefaults is ultimately backed by a file in your app’s container and your app’s container is subject to data protection. If you do nothing special then, on iOS 7 and later, your container uses NSFileProtectionCompleteUntilFirstUserAuthentication, a value that’s inherited by the NSUserDefaults backing store, and so you can’t access it prior to first unlock. > > IMO the best way around this is to avoid NSUserDefaults for stuff that you rely on in code paths that can execute in the background. Instead store those settings in your own preferences file, one whose data protection you can explicitly manage (in this case that means ‘set to NSFileProtectionNone’). > > There are two problems with NSUserDefaults in a data protection context: > > Its a fully abstract API: the presence and location of its backing store is not considered part of that API, so you can’t explicitly manage its data protection. > > Note On recent versions of OS X NSUserDefaults is managed by a daemon and folks who try to manipulate its backing store directly have run into problems. It’s easy to imagine the same sort of thing coming to iOS at some point. > > Even if changing the data protection were possible, NSUserDefaults has no mechanism to classify data based on the context in which you’re using it; it’s an ‘all or nothing’ API. In your case you don’t want to remove protection from all of your user defaults, just those that you need to access in the background before first unlock. > > Finally, if any of this data is truly sensitive, you should put it in the keychain. Notably, the keychain does have the ability to set data protection on an item-by-item basis.

Source: https://webcache.googleusercontent.com/search?q=cache:sR9eZNHpZtwJ:https://forums.developer.apple.com/thread/15685

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNick CartwrightView Question on Stackoverflow
Solution 1 - IphoneconeybeareView Answer on Stackoverflow
Solution 2 - IphoneleonView Answer on Stackoverflow
Solution 3 - IphoneJohn SmithView Answer on Stackoverflow
Solution 4 - IphoneBojan BozovicView Answer on Stackoverflow
Solution 5 - IphoneDerek KnightView Answer on Stackoverflow
Solution 6 - IphoneGrand MView Answer on Stackoverflow