Skip to content

Instantly share code, notes, and snippets.

@shinyquagsire23
Created July 18, 2023 02:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinyquagsire23/9b1d4737ab9857d058f166d9c3046b19 to your computer and use it in GitHub Desktop.
Save shinyquagsire23/9b1d4737ab9857d058f166d9c3046b19 to your computer and use it in GitHub Desktop.
Fixup iOS apps to run on visionOS sim
#!/bin/zsh
PWD=$(pwd)
app_path=$1
app_basename="${app_path%.*}"
if [ "$#" -ne 1 ]; then
echo "Usage: ./fixup.sh YourApp.app"
exit -1
fi
function fixup_build_ver ()
{
which_dylib=$1
vtool_src=$1
vtool_dst=$1
parentpath="$(dirname "$which_dylib")"
vtool -remove-build-version macos -output $vtool_dst $vtool_src &> /dev/null
vtool -remove-build-version ios -output $vtool_dst $vtool_src &> /dev/null
vtool -remove-build-version xrossim -output $vtool_dst $vtool_src &> /dev/null
vtool -remove-build-version iossim -output $vtool_dst $vtool_src &> /dev/null
#vtool -set-build-version xrossim 1.0 1.0 -tool ld 902.11 -output $vtool_dst $vtool_dst &> /dev/null
vtool -set-build-version iossim 14.0 16.2 -tool ld 820.1 -output $vtool_dst $vtool_dst &> /dev/null
plutil -replace UIDeviceFamily -json '[1,2]' $parentpath/Info.plist
plutil -replace CFBundleSupportedPlatforms -json '["XRSimulator"]' $parentpath/Info.plist
plutil -replace MinimumOSVersion -json '1.0' $parentpath/Info.plist
plutil -replace DTSDKName -json '"xrsimulator1.0.internal"' $parentpath/Info.plist
plutil -replace DTPlatformName -json '"xrsimulator"' $parentpath/Info.plist
plutil -replace DTPlatformVersion -json '1.0' $parentpath/Info.plist
codesign -s - $1 --force --deep &> /dev/null
}
function trim() {
local var="$*"
# remove leading whitespace characters
var="${var#"${var%%[![:space:]]*}"}"
# remove trailing whitespace characters
var="${var%"${var##*[![:space:]]}"}"
printf '%s' "$var"
}
fixup_build_ver_recursive() {
for i in "$1"/*;do
if [ -d "$i" ];then
#echo "dir: $i"
fixup_build_ver_recursive "$i"
elif [ -f "$i" ]; then
macho_hdr_1="cffaedfe" # normal
macho_hdr_2="cafebabe" # fat
hdr=$(head -c 4 $i | hexdump -e '16/1 "%02x" "\n"')
hdr=$(trim $hdr)
#echo $i $hdr $macho_hdr_1
if [[ "$hdr" = "$macho_hdr_1" || "$hdr" = "$macho_hdr_2" ]]; then
echo Fixed "$i"
fixup_build_ver $PWD/$i
fi
fi
done
}
#fixup_build_ver $app_path/$app_basename
rm -rf $app_path/PlugIns
echo "Fixing build versions for \`$app_path\`..."
fixup_build_ver_recursive $app_path
mkdir -p $app_path/PlugIns
echo "Signing \`$app_path\`"
codesign -s - $PWD/$app_path --force --deep --verbose
echo "Installing \`$app_path\`"
xcrun simctl install booted $app_path
@samhenrigold
Copy link

Thanks for publishing this! I kept running into an issue with this script in fixup_build_ver_recursive() that would cause it to fail if it encountered an empty directory. I got around this by replacing the i in "$1"/* loop with find:

fixup_build_ver_recursive() {
 find "$1" -type f -print0 | while IFS= read -r -d '' i; do
    macho_hdr_1="cffaedfe" # normal
    macho_hdr_2="cafebabe" # fat

    hdr=$(head -c 4 "$i" | hexdump -e '16/1 "%02x" "\n"')
    hdr=$(trim "$hdr")
    if [[ "$hdr" = "$macho_hdr_1" || "$hdr" = "$macho_hdr_2" ]]; then
      echo Fixed "$i"
      fixup_build_ver "$PWD/$i"
    fi
 done
}

I'm not a script expert though, I'm not sure if this would create any other problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment