Can iOS documents persist through app updates?

My app allows users to create their own documents. iOS stores them in

/var/mobile/containers/Data/Application/FFFF/Documents/


This folder is in the app bundle, yes?


When the user installs an updated build from the app store, they would then lose all of their documents.


How can the app be made to keep the user's documents when a newer build is downloaded from the app store?

A typical update involves first writing the new version of the app to the device, then moving any contents of /Documents over from the original app, then deleting the old app.


In your example, where I assume /FFFF/ is created by you, '/FFFF/Documents' is not promised to be protected - you can't just create /Documents anywhere you choose and expect a normal install to help you.


Is there some reason you don't use /Documents?

iOS stores them in

This looks like the path returned by

NSFileManager
for the
NSDocumentDirectory
directory. If so, items store there should be preserved across updates. If this isn’t working for your app then that’s worth investigating because it works for a bazillion other apps.

This folder is in the app bundle, yes?

No. It looks like a path to the app’s container, not the app’s bundle. Consider this code:

let docDir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let bundleDir = Bundle.main.bundleURL
print("   docDir:", docDir.path)
print("bundleDir:", bundleDir.path)

If you run it on a modern iOS device you get this:

   docDir: /var/mobile/Containers/Data/Application/834D6255-CEB6-429E-9F0E-C24E2161BB66/Documents
bundleDir: /var/containers/Bundle/Application/0DA395D2-C136-4ECD-B422-7EAEAED23593/xxsi.app

Note that the app’s container is within

/var/mobile/
, which you can think of as the user’s home directory. In contrast, the app’s bundle is in (the confusingly named)
/var/containers/
.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hej

hi i have this error Message when **** my app on a device

'/private/var/containers/Bundle/Application/1372BFA2-90C5-4F48-B671-33860246FC58

need your help to fix this

Can iOS documents persist through app updates?
 
 
Q