SuperAPI AI 工具集

run-simulator

编程与开发

查看项目

更新于 10-16 08:08

技能介绍

用途:该技能在 iOS 模拟器中启动真实应用并驱动到用户可见的状态,用运行截图证明改动生效;构建只能证明代码可编译,本技能证明它能运行。它不针对特定应用硬编码,scheme、模拟器、产物路径与 bundle id 均在运行时发现。

适用时机:用户要求“运行”“启动”或“打开”模拟器中的应用;想看到改动生效而非仅编译;要求运行中应用的截图;想在提交前目视验证 UI 修复;对刚改过的视图或流程问“这真的能用吗”。不适用于单元/UI 测试运行(xcodebuild test),那是另一个目标。

主要步骤:1) 发现工程与 scheme,优先 workspace 而非裸 project:ls *.xcworkspace 2>/dev/null || ls *.xcodeproj 2>/dev/null,再用 xcodebuild -list -project <App>.xcodeproj 列出 scheme,选择与应用同名的 scheme,若多个候选且不明确应询问用户;2) 选择模拟器 destination,不要硬编码设备名:xcrun simctl list devices booted 查看已启动设备,再用 xcrun simctl list devices available | grep -i iphone 列出可用 iPhone,优先已启动设备;3) 构建:xcodebuild build -project <App>.xcodeproj -scheme <Scheme> -destination 'platform=iOS Simulator,name=<SimName>',查看输出是否出现 BUILD SUCCEEDED,失败时显示 error 行,不要继续安装陈旧构建;4) 解析 .app 与 bundle id,不要猜 DerivedData 路径,通过 -showBuildSettings 获取 TARGET_BUILD_DIR 与 FULL_PRODUCT_NAME,再用 /usr/libexec/PlistBuddy 读取 Info.plist 中的 CFBundleIdentifier;5) 启动、安装、启动应用:xcrun simctl boot、open -a Simulator、xcrun simctl bootstatus "$SIM" -b 阻塞至完全启动,然后 xcrun simctl install 与 xcrun simctl launch,成功时输出 <bundleid>: <pid>;6) 截图并验证:xcrun simctl io "$SIM" screenshot /tmp/sim-shot.png,然后读取并实际查看截图,空白/白屏、崩溃弹窗或卡在启动屏均视为失败,若改动位于导航之后需先驱动到该界面,可用 xcrun simctl io "$SIM" tap <x> <y> 或 xcrun simctl openurl "$SIM" "<scheme>://<path>",无法精确点击时截取落地屏并告知用户需要点击哪些位置。

输出格式:简要报告运行的 scheme、模拟器名称/OS、bundle id;构建结果(成功/失败及错误尾部);启动结果(成功 pid 或失败原因);截图所示界面及是否确认改动,并嵌入截图;以及用户需自行操作的事项(如到达受限界面所需点击)。

常见失败模式:设备名错误会提示无法找到匹配 destination,应始终列出可用模拟器;陈旧安装导致行为未变时,可执行 xcrun simctl uninstall "$SIM" "$BID" 后重装,或用 xcrun simctl shutdown "$SIM" && xcrun simctl erase "$SIM" 清空(会销毁模拟器数据,需先确认);构建报缺少包/pods 时改用 -workspace <App>.xcworkspace;首次启动较慢,用 simctl bootstatus -b 正确阻塞,固定 sleep 会与安装竞争;冷启动时应用可能仍在初始化,稍等后重新截图再判定失败。参考资料包括 xcrun simctl help、man xcodebuild 以及 Apple 官方文档《Running your app in Simulator》:https://developer.apple.com/documentation/xcode/running-your-app-in-the-simulator-or-on-a-device。