Monday, September 17, 2018

Access to iOS Device Sandbox Data

If you have some files, database for example, in your iOS app, you may need to access to them. Recently, while I was working my app update, I was not sure if a major change would cause to lose my database. Before I run the app on my iPhone, I would like to keep a copy of my database to avoid any risk to lose my data over years.

There are couple ways to do that.

Backup to iTunes

This is very common way to keep a backup of a whole iOS device. First lunch iTunes on a Mac/Windows. Then connect your iPhone to the Mac/Windows. Click on iPhone icon to open a view, where backup is available.

This is very convenient to keep a backup before any adventure or after a period of time. In case of screwup, you can easily to restore your iPhone back to your previous status.

However, with this method, you could not see any files in your app sandbox, nor you could make any changes.

Devices in Xcode

The second way to make a backup of an app is available in Xcode. First, set your iPhone as target or active scheme. From the menu of Windows->Devices and Simulators, open a view.

Here is the view:

Select the app you want to make a backup, then click the gear icon to bring up a menu list. In this way, the whole app sandbox is backed up to your local Mac. The content of the backup can be viewed through Show Package Contents context menu.

This is pretty cool! Not only I can easily to restore the sandbox back to my iPhone, I could also view the content what I have in the app on my iPhone device. For example, the core data database, as you can see, is actually based on several files, all beginning with the same prefix name, defaultDatabase, as I choose it as database name for my app.

When I run my app in Xcode simulator, not only I could view, but also I could access to my app sandbox from Finder. There is no need to download or upload files to my app sandbox in Simulator.

I think that I could make some changes in the backup and restore back with changes I need. This is really handy for iOS developers in case needed.

Notice that you can only access to the app you are in development and installed to the device from Xcode, and the app has to be on your device. You can not see any other apps. If your app is installed through App Store, or Test Flight, you could not see it. This is for security reason. Anyway, it is good enough for me.

The Xcode I use is version 9.4 and iOS is version 11.4.