force reset NSUserDefault on iPhone App upgrade - iphone

i want to force reset to NSUserDefault whenever user update my app. why i need this because every update include some new information abt user. as some info (token) already present in NSUserDefault my app does not call to my web service. due to that fact i dont have new user info. and also i dont want to write if..else statement for every new release.
thanks so much. hope my question is pretty clear.

Check this out:
[NSUserDefaults resetStandardUserDefaults]
For more info check the NSUserDefaults Class reference.
What you can do is save on the defaults the current version of your app. All the time that the user open the app you check the saved version against the current version. In the case of an update the current version will be different from the saved version, and so you know that it's time to clean the user defaults. After cleaned, you save the new value for the current version.

You can save your current application version into NSUserDefaults when you start up your application. Before doing so, you can just do a check to see if the actual application version is greater than the version stored in NSUserDefaults. If it is, you know that the user has just upgraded, and you can remove and information (such as the token) from the defaults using removeObjectForKey

Related

How can I refresh nsuserdefaults value after restore iphone via itunes?

I store the device token value with NSUserdefaults, and then, send this value to my server.
My app checks if this value exists or not.
If the value doesn't exist, my app tries to call the registerdevicetoken method.
But I found out an issue.
If a user restores his/her iPhone via iTunes, the NSUserdefaults values are recovered.
However the stored device token is not a valid device token for a new device, but my app can't recognize that the phone has been reinstalled via iTunes.
How can I collect the correct device token, when users restore their iPhone?
Okay. Let's take the document's folder approach. Firstly, let's take a look at the anatomy of an iOS app. As you see below there are 3 key folders.
The complete Documents and Libray folders get backed up, unless you specifically call a command on a file not to. That's what we will do. Create a file that you can call firstLaunch.txt for example. Put this file into Library/Application support/. And set the noBackUp tag using:
-[NSURL setResourceValue:forKey:error:] using the NSURLIsExcludedFromBackupKey key. This will prevent the file from backing up. Now, you can have a simple if-else statement checking if the file exists. If it does, that means that the app was already run and the token is up to date. If it does not, create a new user token and now create the file again, put it into the correct folder. To be clear, this is the only time you should be creating the file, as it will only get called on the first launch and after being restored. You can find more info and this image at the Apple Docs.
Hope that helps, Julian.

Would the value of NSUserDefault lost after upgrade the app?

Would the value of NSUserDefaults lost after upgrade the app?
If I delete the app and reinstall it ,and then restore data from Itunes backup, would the value of NSUserDefaults lost?
You don't lose the value of NSUserDefaults when you upgrade the app.
When you delete and reinstall the app, though, you reset all the NSUserDefaults keys, so you can lose data (i.e. your score, etc).
Restoring from backup doesn't lose the NSUserDefaults value, due you've stored the value in iTunes and iTunes is giving you back again that value.
iCloud is the better solution unless you have some reason for not using it, and has the advantage of synchronizing multiple installations by the same user should you so desire.

How persistent is [NSUserDefaults standardUserDefaults]?

I'm using [NSUserDefaults standardUserDefaults] for storing application settings.
My questions are:
do those settings are removed on app deletion?
are they kept after an application update (through the
AppStore)?
Because I'm using it to store a password and don't want my users to reset them at each update. Also, I'd like that the only way to reset the password would be to remove the app and re-install it.
Is NSUserDefault the right choice?
Thanks,
Jérémy
Yes, they are removed on app deletion and yes they are kept when an application is updated.
However, you're not advised to store sensitive data in the NSUserDefaults, instead I would look at using the Keychain.
I use NSUserDefaults in my app to allow additional access to my app for my colleagues. They just have to enter the code word in settings and the app is fully opened.
to the point each time I update the app they have to re-enter the code word, so I would say from experience that they are not kept after updates. The values need to be re-entered.

iPhone, Where do I have to save user generated files if I want them to stay there after an app Upgrade (new release)? is Documents directory good?

I have an app that lets user record their own audio.
By now I'm saving those files into Documents directory.
My question is: if I will release a new version of that app, will user recorded files get deleted?
Is there a better place to store user generated audio files?
Should I use NSUserDefaults for data that stay even after app upgrade?
thx
NSUserDefaults is used for storing settings (objects of Key-Value Coding compliant classes)
Other data such files you should store in Documents folder, wich survive between updates (if you don't delete it yourself, of course :)
Both the Documents directory and NSUserDefaults survive application updates. Choosing which to use depends on the kind of your data.
if I will release a new version of that app, will user recorded files get deleted?
No.
Is there a better place to store user generated audio files?
No.
Should I use NSUserDefaults for data that stay even after app upgrade?
Only if your data is small.

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

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.
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.
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.
Direct answer to the posted question: YES.
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.
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...
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.
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 (eskimo1#apple.com):
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