iOS: how to save text file and prevent deletion on app update?

I can save a config text file with the following code, however the file is deleted with each update of the app. I need to be able to export a text file with config information outside of the standard app folders, so that I can import it again, after the app was updated.

This code saves the config, but it is not protecting it from updates:

Var json As String = im.ConfigToJson
Var f As FolderItem = SpecialFolder.Documents.Child("config.txt")
Var tos As TextOutputStream = TextOutputStream.Create(f)
tos.WriteLine json
tos.Close

I imagine I could save it in a way that lateron it could be found with the files app “on my iPhone”.

How do you do such thing?

That shouldn’t happen to documents in the documents folder.
Certainly doesnt happen to my apps, or I would have been swamped with complaints.
If an app is deleted, then the documents folder would be trashed.

I suspect what you are seeing is happening in debug mode, where a fresh compile can get a new temporary sandbox, in whch the documents folder will be empty?

You are right! I got confused, because when I used the macOS Finder and went to look for files in the connected iPhone, then the textfile was not immediately displayed. But after disconnecting and reconneting the iPhone to the Mac, then the file reappeared. So it looks like it works as I need it.

Thanks for your reply!

How much data are you saving @Oliver_Osswald ?

The standard way to save config data is through NSUserDefaults key/value pairs on iOS.

NSUserDefaults will enable saving/reading small chunks of String, Numbers, Boolean etc.
Values are cached by the system and regularly written to disk as a whole.

I use NSUserDefaults to store the preferred language, user_identifier, app preferences and so on. Overall data is usually less than 10kB.

If the app is unloaded / quickly deleted, the data stored in NSUserDefaults will usually have a backup on iCloud. (hidden back-up, user can’t access it).

As soon as the app is installed again, the NSUserDefaults key/value pairs will be restored.

If you would like to use NSUserDefaults, iOSKit has it built-in.
If you don’t want to use iOSKit, I could extract what is needed and post the Module on Github.

Yes, NSUserDefaults, iOSKit is what I am actually using for normal operation.

However, I wanted to add an option in order to export config data and safely keep it, even if the app was deleted for some time.

1 Like