THERAMPAGE
THERAMPAGE
THERAMPAGE
Switch to the Russian version
Main   |   Blog   |   EngRead   |   Dragon: The Eater   |   Rampage CMS
Directory of the iOS application running in the simulator
Sometimes it happens that you need to see what is in the iOS application directory. What files are stored, how much space they take, etc. But how to do this - it is not quite obvious.
 
The first and perhaps easiest thing is to add the following line somewhere in the application code: 
print("app folder path is (NSHomeDirectory())")
And then look in the console for the result of the execution. The option is certainly not bad, but rather cumbersome: you have to add the line, then find the output, copy the path, open the directory...
 
Another way is to use the following command: 
xcrun simctl get_app_container booted com.example.app data
As a result, the path to the directory we need will be displayed on the screen. This is also not the easiest command, but this can be improved, which we will do a little later, but for now we will consider the third option:
 
An app called RocketSim for Xcode Simulator [https://apps.apple.com/app/apple-store/id1504940162] A fantastic development tool that adds all kinds of helper features to the iOS simulator, including the ability to quickly navigate to the application directory. Highly recommended, but there is a caveat, it is only available for MacOS 12.4 and higher.
 
But let's go back to the previous command and see how we can improve it. At first, put it in a shell script, then add a convenient way to set the Bundle Id, process the results and display them or immediately open the directory we need.
 
#!/bin/sh
DEVICE_IDS="$(xcrun simctl list | grep Booted | awk -F '[()]' '{print $2}')"
while read -r line; do DEVICE_IDS_ARRAY+=("$line"); done <<<"$DEVICE_IDS"
DEVICE_NUMBER="${#DEVICE_IDS_ARRAY[@]}"
if [ "$DEVICE_NUMBER" = 1 ]; then
    DEVICE_ID=$DEVICE_IDS
else
    DEVICE_NAMES="$(xcrun simctl list | grep Booted | awk -F '[()]' '{print $1}')"
    while read -r line; do DEVICE_NAMES_ARRAY+=("$line"); done <<<"$DEVICE_NAMES"
    i=0
    for element in "${DEVICE_NAMES_ARRAY[@]}"
    do
        i=$((i+1))
        echo "$i) $element"
    done
    read -p "Choose which one: " DEVICE_POSITION
    DEVICE_ID=${DEVICE_IDS_ARRAY[DEVICE_POSITION-1]}
fi
echo "DEVICE_ID: ${DEVICE_ID}"
if [ -z "${DEVICE_ID}" ]; then
    echo "No running simulators have been found"
    exit 1
fi
getopts ":fFtT" CHOICE;
shift $((OPTIND - 1))
APP_NAME=$1
if [ -z "${APP_NAME}" ]; then
    read -p "Enter some part of an app budle id: " APP_NAME
fi
echo "APP_NAME: ${APP_NAME}"
APP_PATH=~/Library/Developer/CoreSimulator/Devices/$DEVICE_ID/data/Containers/Data/Application
echo "APP_PATH: ${APP_PATH}"
cd $APP_PATH
APP_ID=$(find . -iname *$APP_NAME* | head -n 1 | awk -F '/' '{print $2}')
echo "APP_ID: ${APP_ID}"
if [ -z "${APP_ID}" ]; then
    echo "Unable to find an app which bundle id contains '$APP_NAME'"
    exit 1
fi
echo "The path is:\n\n$APP_PATH/$APP_ID\n"
if [ -z "${CHOICE}" ] || [ "$CHOICE" = "?" ]; then
    read -p "Open [T]erminal or [F]inder window?: " CHOICE
fi
if [ "$CHOICE" = "T" ] || [ "$CHOICE" = "t" ]; then
    open -a Terminal "$APP_PATH/$APP_ID"
fi
if [ "$CHOICE" = "F" ] || [ "$CHOICE" = "f" ]; then
    open "$APP_PATH/$APP_ID"
fi
 
Download the script openSimDir.sh