Matt Coneybeare

MC

Programmatically Opening the iOS Simulator Data Directory for Your App

| Comments

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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:

Example2

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 it’s root directory (i.e. app folder) then open up it’s 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

My name is Matt Coneybeare, I design and develop for iOS (iPhone, iPad and iPod Touch), Mac OS X and the Web out of New York. In 2008 I started a software company called Urban Apps that has made some pretty popular apps such as Ambiance and Hourly News. My current Stack Overflow reputation is about 27k.

I was a Rockstar a decade ago, but then went back to school and collected a Bachelor's Degree in Computer Science from U.C. Berkeley. Now I am settled down with my beautiful wife Di and our two doggies Hamachi and Foxy. While coding, I walk several miles/day on my Treadmill Desk. When not at my desk, I love exploring New York City as a Yelp Elite, or training for marathons.

Contact information

Name
Matt Coneybeare
Email
Website
Twitter
Instagram
GitHub
LinkedIn
Google+