Programmatically Opening the iOS Simulator Data Directory For Your App

1 minute read

When developing an app that downloads data and/or creates a lot of data, it is useful to inspect the simulator's file system to ensure the files are what, and where you expect them to be. I was tired of having to manually dig in and figure out where these files were being installed to, so I wrote an Xcode Run Script to figure it out for me.

if [ ${PLATFORM_NAME} = "iphonesimulator" ]
then
  # Get the base directory
  SIMULATOR_BASE_DIR="/Users/$USER/Library/Application Support/iPhone Simulator/"
  # Check if it exists
  if [ -d "$SIMULATOR_BASE_DIR" ]; then
    # Get the most recent install, i.e. the one we just installed
    MOST_RECENT_APP=`find "$SIMULATOR_BASE_DIR" -name "$FULL_PRODUCT_NAME" -type d -mtime -365 -print0 | xargs -0 stat -f "%m %N" | sort -rn | head -1 | cut -f2- -d" "`
    # Get the containing folder
    MOST_RECENT_APP_DIRECTORY=`dirname "$MOST_RECENT_APP"`
    # Get the data folder
    MOST_RECENT_APP_DATA_DIRECTORY="$MOST_RECENT_APP_DIRECTORY/Library/Application Support/$PRODUCT_NAME"
    # Check if it exists
    if [ -d "$MOST_RECENT_APP_DATA_DIRECTORY" ]; then
      # Echo it for debugging
      echo "Opening $MOST_RECENT_APP_DATA_DIRECTORY"
      # Open it
      open -g "$MOST_RECENT_APP_DATA_DIRECTORY"
    fi
  fi
fi

Install it as a run script in your Xcode Target like so:

It works by using the find command to search the iOS Simulator's file system for the product name, then grabs the most recently modified one. If this run script is added after the copy files script, we can be assured that the most recently modified one is the one we just built. Once we have the most recent product, we find its root directory (i.e. app folder) then open up its Application Support directory. You may need to edit the MOST_RECENT_APP_DATA_DIRECTORY= line to suit the directory structure for your specific project. Enjoy

Lastly, I run a small software company called Urban Apps. It pays the bills so I can take the time to write helpful posts like this one. If you found this posting helpful at all, I would really appreciate it if you would check out my Apps on the iTunes App Store.

Comments