Run iOS Simulator from the Command Line (runsim)

本文提供了一个脚本,用于在iOS模拟器中安装应用。只需将脚本放置在App所在的目录下,并指定应用的可执行文件名称。脚本支持安装并启动iPhone或iPad应用,同时允许更改模拟器版本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 Use the following script to install app in simulator. Just put the script under the MyApp.app directory. 

To use the script, specify the name of your executable: $ runsim -iphone /path/app

One thing that might need changing is the version number of the simulator that you want to run, currently using 6.0 but this will also work with 5.1 and 5.0.

#!/bin/bash
#
# runsim   Install and run apps in the iOS Simulator
#
# Copyright (c) 2012 Psellos   http://psellos.com/
# Licensed under the MIT License:
#     http://www.opensource.org/licenses/mit-license.php
#
USAGE='usage:  runsim  [ -i { phone | pad } ] [ -srdl ]  executable  file ...'
#
# -iphone   Install as iPhone app
# -ipad     Install as iPad app
# -s        Start iOS Simulator
# -r        Run the app in the simulator
# -d        Delete the installed app
# -l        List names of installed apps
#
# file ...  Additional files to install with the executable
#
# Default flags are -iphone -s (install as iPhone app and start simulator).
#
# Currently the -r flag uses Instruments and thus requires
# authentication as a member of the _developer group.
#
VERSION=2.0.0

INSTALL=n
START=n
RUN=n
DELETE=n
LIST=n
while getopts i:srdl opt; do
    case "$opt" in
    i)
        INSTALL=y
        case "$OPTARG" in
        phone) FAMILY=1 ;;
        pad) FAMILY=2 ;;
        *)
            echo "runsim: unrecognized device family: $OPTARG" >&2
            echo "$USAGE" >&2
            exit 1
            ;;
        esac
        ;;
    s) START=y ;;
    r) RUN=y ;;
    d) DELETE=y ;;
    l) LIST=y ;;
    ?) echo "$USAGE" >&2; exit 1 ;;
    esac
done
shift $(($OPTIND - 1))

case "$INSTALL$START$RUN$DELETE$LIST" in nnnnn)
    INSTALL=y
    FAMILY=1
    START=y
esac

if [ "$INSTALL$RUN$DELETE" != nnn -a $# -lt 1 ]; then
    echo 'runsim: need an executable name for -i -r or -d' >&2
    echo "$USAGE" >&2
    exit 1
fi
EXEC="$1"
shift

APPDIR="$HOME/Library/Application Support/\
iPhone Simulator/6.0/Applications"

TRCSUB=Contents/Applications/Instruments.app\
/Contents/PlugIns/AutomationInstrument.bundle\
/Contents/Resources/Automation.tracetemplate

xcodeloc() {
    # Get location of Xcode, otherwise use default
    if [ -f runsim.xcloc ]; then
        cat runsim.xcloc
    else
        echo /Applications/Xcode.app
    fi
}

appuuid() {
    # Get UUID for an app. If installed, re-use existing one. Otherwise
    # create a new one and return it.
    #
    for f in "$APPDIR"/*/"$1.app" ; do
        if [ -d "$f" ]; then
            basename "$(dirname "$f")"
            return 0
        fi
    done
    uuidgen
}

install() {
    # Install executable $EXEC and associated files into simulator's
    # file system.
    #

    # Figure out startup file, if any. If a nibfile or storyboard file
    # is given, the first one is the startup file. Otherwise if there's
    # a file $EXEC.nib or $EXEC.storyboard, that is the startup file.
    # Otherwise there is no startup file.
    #
    NIBFILE=
    STORYFILE=
    if [ -f "$EXEC.nib" ]; then
        NIBFILE="$EXEC"
    elif [ -f "$EXEC.storyboard" ]; then
        STORYFILE="$EXEC"
    fi
    for f ; do
        case "$f" in
        *.nib)
            STORYFILE=; NIBFILE="$(basename "$f" .nib)"; break ;;
        *.storyboard)
            NIBFILE=; STORYFILE="$(basename "$f" .storyboard)"; break ;;
        esac
    done

    UUID=$(appuuid "$EXEC")

    # Install app and associated files.
    #
    TOPDIR="$APPDIR/$UUID"
    mkdir -p "$TOPDIR"
    mkdir -p "$TOPDIR/Documents"
    mkdir -p "$TOPDIR/Library"
    mkdir -p "$TOPDIR/tmp"
    mkdir -p "$TOPDIR/$EXEC.app"

    cp "$EXEC" "$TOPDIR/$EXEC.app"

    if [ "$NIBFILE" != "" ]; then
        cp "$NIBFILE.nib" "$TOPDIR/$EXEC.app"
    elif [ "$STORYFILE" != "" ]; then
        cp "$STORYFILE.storyboard" "$TOPDIR/$EXEC.app"
    fi

    # If an Info.plist exists, use it.  Otherwise make one.
    if [ -f Info.plist ] ; then
        plutil -convert xml1 -o "$TOPDIR/$EXEC.app/Info.plist" Info.plist
    else
        cat > "$TOPDIR/$EXEC.app/Info.plist" <<HERE1
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"\
 "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>CFBundleDevelopmentRegion</key>
        <string>English</string>
        <key>CFBundleDisplayName</key>
        <string>$EXEC</string>
        <key>CFBundleExecutable</key>
        <string>$EXEC</string>
        <key>CFBundleIconFile</key>
        <string>Icon.png</string>
        <key>CFBundleIdentifier</key>
        <string>com.example.$EXEC</string>
        <key>CFBundleInfoDictionaryVersion</key>
        <string>6.0</string>
        <key>CFBundleName</key>
        <string>$EXEC</string>
        <key>CFBundlePackageType</key>
        <string>APPL</string>
        <key>CFBundleSignature</key>
        <string>????</string>
        <key>CFBundleShortVersionString</key>
        <string>1.0</string>
        <key>CFBundleVersion</key>
        <string>1.0.0</string>
        <key>UIStatusBarStyle</key>
        <string>UIStatusBarStyleBlackOpaque</string>
        <key>LSRequiresIPhoneOS</key>
        <true/>
HERE1
        if [ "$NIBFILE" != "" ]; then
            cat >> "$TOPDIR/$EXEC.app/Info.plist" << HERE2
        <key>NSMainNibFile</key>
        <string>$NIBFILE</string>
HERE2
        elif [ "$STORYFILE" != "" ]; then
            cat >> "$TOPDIR/$EXEC.app/Info.plist" << HERE3
        <key>NSMainStoryboardFile</key>
        <string>$STORYFILE</string>
HERE3
        fi
        cat >> "$TOPDIR/$EXEC.app/Info.plist" <<HERE4
</dict>
</plist>
HERE4
    fi

    # Add device specifications to Info.plist (normally done by Xcode).
    # Without these, Instruments reports the app as AWOL.
    #
    python -c '
import plistlib
import sys
p = plistlib.readPlist(sys.argv[1])
p["CFBundleSupportedPlatforms"] = ["iPhoneSimulator"]
p["DTPlatformName"] = "iphonesimulator"
p["DTSDKName"] = "iphonesimulator6.0"
p["UIDeviceFamily"] = ['$FAMILY']
plistlib.writePlist(p, sys.argv[1])
' "$TOPDIR/$EXEC.app/Info.plist"

    echo -n 'AAPL????' > "$TOPDIR/$EXEC.app/PkgInfo"

    # Install conventional image files if they exist.
    #
    if [ -f Icon.png ]; then
        cp Icon.png "$TOPDIR/$EXEC.app"
    fi
    if [ -f Default.png ]; then
        cp Default.png "$TOPDIR/$EXEC.app"
    fi

    # Install any other given files.
    #
    for f; do
        if [ "$f" = "$NIBFILE.nib" ]; then continue; fi
        if [ "$f" = "$STORYFILE.storyboard" ]; then continue; fi
        cp "$f" "$TOPDIR/$EXEC.app"
    done
}


start() {
    # Start the iOS Simulator
    #
    open "$(xcodeloc)"/Contents/\
Developer/Platforms/iPhoneSimulator.platform/\
Developer/Applications/iPhone\ Simulator.app
}


run() {
    # Run the app inside iOS Simulator by asking Instruments to trace it
    # with null trace. If you haven't agreed to the licensing terms of
    # Xcode, this will fail until you do.  The first time in each login
    # session, this will ask for authentication as an admin or
    # developer.
    #
    TOPDIR="$APPDIR/$(appuuid "$EXEC")"
    if [ ! -d "$TOPDIR/$EXEC.app" ]; then
        echo "runsim: app \"$EXEC\" not installed" >&2
        exit 1
    fi
    (instruments -D /tmp/runsim$$.trace -t "$(xcodeloc)/$TRCSUB" \
            "$TOPDIR/$EXEC.app" < /dev/null 2>&1 > /dev/null | \
            grep 'xcodebuild -license' >&2 ; \
        rm -rf /tmp/runsim$$.trace) &
}

delete() {
    # Delete an installed app.
    #
    TOPDIR="$APPDIR/$(appuuid "$EXEC")"
    if [ ! -d "$TOPDIR" ]; then
        echo "runsim: app \"$EXEC\" not installed" >&2
        exit 1
    fi
    rm -rf "$TOPDIR"
}

list() {
    # List installed apps.
    #
    for f in "$APPDIR"/*/*.app ; do
        if [ -d "$f" ]; then
            basename "$f" .app
        fi
    done
}


case $INSTALL in y) install "$@" ;; esac
case $START in y) start ;; esac
case $RUN in y) run ;; esac
case $DELETE in y) delete ;; esac
case $LIST in y) list ;; esac


The lite version is

#Here is the script which just installs your app to the simulator.

# Pick a uuid for the app (or reuse existing one).
if ! [ -f installApp.uuid ]; then
uuidgen > installApp.uuid
fi
UUID=$(cat installApp.uuid)
#create supporting folders
TOPDIR="$HOME/Library/Application Support/\
iPhone Simulator/6.0/Applications/$UUID/"
mkdir -p "$TOPDIR"
mkdir -p "$TOPDIR/Documents"
mkdir -p "$TOPDIR/Library"
mkdir -p "$TOPDIR/tmp"
mkdir -p "$TOPDIR/$1.app"

#copy all the app file to the simulators directory
cp -r * "$TOPDIR/$1.app"

# Get location of Xcode, otherwise use default
if [ -f installApp.xcloc ]; then
XCLOC="$(cat installApp.xcloc)"
else
XCLOC=/Applications/Xcode.app
fi

refer from:http://psellos.com/2012/05/2012.05.iossim-command-line-2.html


### iOS Simulator 使用指南和常见问题解决方案 #### 一、iOS Simulator 的基本操作 对于开发者而言,掌握如何高效利用 iOS Simulator 是十分重要的。启动模拟器可以通过 Xcode 或命令行完成。Xcode 提供了一个直观的图形界面来管理不同设备类型的仿真环境[^2]。 ```bash xcrun simctl list devices ``` 上述命令可以列出当前可用的所有虚拟设备列表。通过指定特定的设备 ID 和操作系统版本号,还可以创建新的模拟实例: ```bash xcrun simctl create "My iPhone" com.apple.CoreSimulator.SimDeviceType.iPhone-8 com.apple.CoreSimulator.SimRuntime.iOS-14-5 ``` #### 二、构建适用于模拟器的应用程序 为了使应用程序能够在 iOS Simulator 上正常运行,在编译过程中需要注意一些细节。特别是当涉及到静态库时,建议采用 `XCFramework` 来打包资源文件,从而确保兼容性并减少潜在错误的发生概率。 #### 三、处理动态链接失败的情况 有时可能会遇到类似于 “Failed to lookup symbol (dlsym(RTLD_DEFAULT, test_func): symbol not found)” 这样的报错信息。这通常意味着目标函数未能成功加载到内存中。针对此类情况的一个有效对策是在项目设置里调整 Linking 配置项下的 Other Linker Flags 参数,加入 `-all_load` 或者 `-force_load` 标志位以强制载入所有对象文件中的符号表数据[^3]。 #### 四、跨平台开发的支持 尽管官方推荐 macOS 平台作为主要的工作站来进行 iOS 应用的研发工作;然而借助第三方工具集的帮助——比如 WinObjC ——也可以实现在 Windows 系统环境下编写 Objective-C/C++ 源码,并且能够顺利连接至远程真机或是本地安装好的 iOS Simulator 实例进行测试验证活动[^4]。 #### 五、网络诊断辅助功能 如果遇到了与网络通信有关的问题,则可考虑引入专门设计用于排查这类故障的服务端组件或客户端 SDK 。例如 GitHub 上开源发布的 **iOS-netdiag** 工具包就提供了丰富的 API 接口和服务端配置选项,帮助快速定位并修复由网络引起的异常状况[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值