iOS app data files preinstalled into sandbox?

I'm working on an iOS iPhone app that requires two large (~200MB) bundled data files. I've added the data files to my Xcode project ("Target Membership").

When Xcode installs the app onto the iPhone, it does copy the data files over, but they don't appear in the app sandbox at runtime (e.g., /var/mobile/Containers/Data/Application/1485D9CE-B1EF-3A24-9611-43C6CD368572/Documents).

Q1. Where are the data files located, and how can my app access them?

Q2. Can I have Xcode install these files directly into the app's sandbox?

Answered by DTS Engineer in 730936022

Can my app modify the file contents in the main bundle?

No. Bundles are always read-only on iOS [1].

Or would it have to copy the file from the main bundle into the sandbox and then modify/update it?

That’s not a bad approach for a small file. However, it’s not a great choice given that size of these files. Just running your app for the first time would eat 400 MB of the user’s storage to no benefit. I’ve seen App Review reject folks for doing similar things.

For something this large, best practice is to implement a layered approach. That is:

  • Create a database in the Documents folder.

  • Query that and then, if there’s nothing there, try the read-only copy in the app’s bundle.

This can get quite tricky, depending on the nature of your data.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] See Separate Read-Only and Read/Write Content in Embedding Nonstandard Code Structures in a Bundle.

Okay, I found the files in:

/private/var/containers/Bundle/Application/26FA4220-0E34-43EF-AC23-0F46CABF906E/.app/dataset1.dat

And I understand now that such "project" files don't go into the ../Documents folder.

Q: Can my app modify the file contents in the main bundle? Or would it have to copy the file from the main bundle into the sandbox and then modify/update it?

Accepted Answer

Can my app modify the file contents in the main bundle?

No. Bundles are always read-only on iOS [1].

Or would it have to copy the file from the main bundle into the sandbox and then modify/update it?

That’s not a bad approach for a small file. However, it’s not a great choice given that size of these files. Just running your app for the first time would eat 400 MB of the user’s storage to no benefit. I’ve seen App Review reject folks for doing similar things.

For something this large, best practice is to implement a layered approach. That is:

  • Create a database in the Documents folder.

  • Query that and then, if there’s nothing there, try the read-only copy in the app’s bundle.

This can get quite tricky, depending on the nature of your data.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] See Separate Read-Only and Read/Write Content in Embedding Nonstandard Code Structures in a Bundle.

I guess it would it make sense to:

  • Deliver the app, but without the data files in the main bundle
  • Have the user download the files upon first run of the app into ~/Documents

Then the user could update at any time by downloading new data files.

That only makes sense if the data is optional. If the user always has to download the data to use your, you’re back in the position of eating 400 MB of the user’s disk space even though they’ve not done any real work with your app.

Keep in mind that consuming 400 MB in the Documents directory is worse than 400 MB in your app, but the system backs up the former but not the latter.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

iOS app data files preinstalled into sandbox?
 
 
Q