I had a post a long time ago describing the usage of directory hardlinks in order to easily access, backup and restore the container of your application on the iOS Simulator: Persistent application container directories in iOS Simulator. Obviously now it’s an APFS era (enforced by apple) and I’m not sure if it has directory hardlinks anymore. So I have another, simpler way to backup app containers.
Thanks to simctl, it’s now very easy to find the container directory of your program in any iOS Simulator. I typically work with only one and simctl understands the string booted for the currently booted simulator.
To backup your application’s current data, you simply run:
~ $ rsync -av --delete "$( xcrun simctl get_app_container booted org.example.app data )/" ~/Desktop/123_incorrect_state/
sending incremental file list
created directory /Users/user/Desktop/123_incorrect_state
sent 13,560,443 bytes received 2,602 bytes 27,126,090.00 bytes/sec
total size is 13,543,239 speedup is 1.00
In the command, org.example.app is the application’s bundle ID, and the last argument is where to store the backup. Remember to include the trailing slashes on both the source and destination directories.
And to restore back, you need to make sure the application is installed on the booted simulator (or install a fresh one and quit it) and then swap the source and destination:
~ $ rsync -av --delete ~/Desktop/123_incorrect_state/ "$( xcrun simctl get_app_container booted org.example.app data )/"<<<sending incremental file list
sent 6,161,528 bytes received 1,991 bytes 12,327,038.00 bytes/sec
total size is 13,543,239 speedup is 2.20
As you see, the --delete flag removes any files in the app container which are not in the saved backup.
These are easy commands that work very fast and help me have a productive development environment where I can restore a known application state again and again.