Reputation: 1
I have an app on both the App Store (iOS - Swift) and Play Store (Android - Java). The app uses UserDefaults.standard for iOS and SharedPreferences for Android to store a few settings.
Both of these implementations work perfectly. The apps can be launched and quit hundreds of times over the course of months and all settings persist.
When I push an update to the stores and install the updates on several devices, the settings are deleted. From what I have read online, UserDefaults and SharedPreferences are supposed to persist between app updates and should only be deleted when the app is uninstalled. However, this is not the case for me. I have also found several posts regarding this issue on many different sites (including this one) but there is never an answer.
Is there some sort of setting, flag, code, or anything else that needs to be set (or not set) in order to ensure that UserDefaults and SharedPreferences persist after an app update?
Are UserDefaults and SharedPreferences what I should be using for this purpose? If not, what should I be using? I have already tried to save a serialized object to file and an xml file but those were deleted after an app update as well. (See: File Deleted After Update)
I do not need any security for these settings. I just need a robust way to have the settings persist between app updates.
Here is the relevant code for iOS followed by Android. After an app update, the tutorial screen is always shown. All other settings are also defaults.
class Settings {
static let shared = Settings()
var tutorialShown = false
private init() {
let defaults = UserDefaults.standard
if defaults.value(forKey: "tutorialShown") == nil {
defaults.set(false, forKey: "tutorialShown")
}
tutorialShown = defaults.bool(forKey:"tutorialShown")
//other settings omitted for brevity
}
func save() {
let defaults = UserDefaults.standard
defaults.set(tutorialShown, forKey:"tutorialShown")
//other settings omitted for brevity
}
}
//somewhere in the code
Settings.shared.tutorialShown = true
Settings.shared.save()
//somewhere in the code
if(Settings.shared.tutorialShown) {
sceneName = "TitleScene"
} else {
sceneName = "WelcomeScene"
}
//And for Android
public class Settings {
//instance
private static Settings instance;
private static Lock instanceLock = new ReentrantLock();
public boolean tutorialShown;
public static Settings getInstance() {
instanceLock.lock();
try {
if (instance == null) {
instance = new Settings();
}
return instance;
} finally {
instanceLock.unlock();
}
}
private Settings() {
Preferences prefs = Gdx.app.getPreferences("packageNameHere");
tutorialShown = prefs.getBoolean("tutorialShown", false);
//other settings omitted for brevity
}
public void save() {
Preferences prefs = Gdx.app.getPreferences("packageNameHere");
prefs.putBoolean("tutorialShown", tutorialShown);
prefs.flush();
}
}
//somewhere in the code
Settings.getInstance().tutorialShown = true;
Settings.getInstance().save();
//somewhere in the code
if(Settings.getInstance().tutorialShown) {
this.setScreen(new TitleScreen(this));
} else {
this.setScreen(new WelcomeScreen(this));
}
Upvotes: 0
Views: 38