using-xcode-cli
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseUsing Xcode CLI
使用Xcode CLI
Overview
概述
Native Xcode CLI tools ( and ) provide full control over iOS/macOS builds and simulators without opening Xcode IDE. Core principle: Use CLI for automation, headless builds, and CI/CD—the same tools Xcode uses internally.
xcodebuildxcrun simctl原生Xcode CLI工具(和)无需打开Xcode IDE即可全面控制iOS/macOS应用的构建和模拟器。核心原则: 使用CLI实现自动化、无头构建以及CI/CD——这些都是Xcode内部使用的同款工具。
xcodebuildxcrun simctlWhen to Use
适用场景
- Building iOS/macOS apps from command line
- Running apps in iOS Simulator programmatically
- Taking screenshots or recording video of simulator
- Running unit/UI tests with specific targeting
- Automating builds in CI/CD pipelines
- Managing simulator instances (boot, shutdown, erase)
- Simulating location, push notifications, or permissions
- Capturing app logs for debugging
Symptoms that trigger this skill:
- "Unable to find destination matching"
- "No scheme named X found"
- "xcodebuild: error:"
- Need to automate Xcode workflows
- Building without opening Xcode IDE
- 从命令行构建iOS/macOS应用
- 以编程方式在iOS Simulator中运行应用
- 为模拟器截取屏幕截图或录制视频
- 针对特定目标运行单元/UI测试
- 在CI/CD流水线中自动化构建
- 管理模拟器实例(启动、关闭、重置)
- 模拟位置、推送通知或权限
- 捕获应用日志用于调试
触发使用该技能的典型场景:
- "无法找到匹配的目标设备"
- "未找到名为X的scheme"
- "xcodebuild: error:"
- 需要自动化Xcode工作流
- 无需打开Xcode IDE即可构建应用
When NOT to Use
不适用场景
- Editing code or project settings → Use Xcode IDE
- Managing Swift Package dependencies → Use CLI
swift package - Cross-platform builds → Use platform-specific tools
- Signing/provisioning profile management → Use Xcode or fastlane
- 编辑代码或项目设置 → 使用Xcode IDE
- 管理Swift Package依赖 → 使用CLI
swift package - 跨平台构建 → 使用对应平台的专属工具
- 签名/描述文件管理 → 使用Xcode或fastlane
Quick Start
快速入门
Find available simulators:
bash
xcrun simctl list devices availableBuild for simulator:
bash
UDID=$(xcrun simctl list devices --json | jq -r '.devices | .[].[] | select(.name=="iPhone 16 Pro" and .isAvailable==true) | .udid' | head -1)
xcodebuild -workspace App.xcworkspace -scheme App -destination "platform=iOS Simulator,id=$UDID" -derivedDataPath /tmp/build buildInstall and launch:
bash
APP_PATH=$(find /tmp/build -name "*.app" -type d | head -1)
xcrun simctl install $UDID "$APP_PATH"
xcrun simctl launch --console $UDID com.bundle.identifierTake screenshot:
bash
xcrun simctl io $UDID screenshot /tmp/screenshot.png查找可用模拟器:
bash
xcrun simctl list devices available为模拟器构建应用:
bash
UDID=$(xcrun simctl list devices --json | jq -r '.devices | .[].[] | select(.name=="iPhone 16 Pro" and .isAvailable==true) | .udid' | head -1)
xcodebuild -workspace App.xcworkspace -scheme App -destination "platform=iOS Simulator,id=$UDID" -derivedDataPath /tmp/build build安装并启动应用:
bash
APP_PATH=$(find /tmp/build -name "*.app" -type d | head -1)
xcrun simctl install $UDID "$APP_PATH"
xcrun simctl launch --console $UDID com.bundle.identifier截取屏幕截图:
bash
xcrun simctl io $UDID screenshot /tmp/screenshot.pngQuick Reference
快速参考
| Task | Command |
|---|---|
| List schemes | |
| List simulators | |
| Get simulator UDID | |
| Boot simulator | |
| Build for simulator | |
| Install app | |
| Launch app | |
| Take screenshot | |
| Run tests | |
| Stream logs | |
| 任务 | 命令 |
|---|---|
| 列出scheme | |
| 列出模拟器 | |
| 获取模拟器UDID | |
| 启动模拟器 | |
| 为模拟器构建应用 | |
| 安装应用 | |
| 启动应用 | |
| 截取屏幕截图 | |
| 运行测试 | |
| 流式传输日志 | |
Reference Documentation
参考文档
| Reference | Contents |
|---|---|
| xcodebuild.md | Project discovery, building, archiving, testing |
| simctl.md | Device management, screenshots, video, location, permissions |
| logging.md | Log streaming and filtering predicates |
| workflows.md | End-to-end automation scripts |
| 参考链接 | 内容 |
|---|---|
| xcodebuild.md | 项目发现、构建、归档、测试 |
| simctl.md | 设备管理、截图、视频、位置、权限 |
| logging.md | 日志流式传输与过滤谓词 |
| workflows.md | 端到端自动化脚本 |
Common Patterns
常见模式
Build + Run
构建 + 运行
- Find simulator UDID via
simctl list devices --json - Boot simulator with
simctl boot $UDID - Build with using
xcodebuild-derivedDataPath - Find .app bundle in derived data
- Install with
simctl install - Launch with
simctl launch --console
- 通过查找模拟器UDID
simctl list devices --json - 使用启动模拟器
simctl boot $UDID - 使用并指定
xcodebuild进行构建-derivedDataPath - 在派生数据中查找.app包
- 使用安装应用
simctl install - 使用启动应用
simctl launch --console
Run Tests
运行测试
bash
xcodebuild -workspace App.xcworkspace -scheme App \
-destination "platform=iOS Simulator,id=$UDID" \
-only-testing "AppTests/SpecificTest" testbash
xcodebuild -workspace App.xcworkspace -scheme App \
-destination "platform=iOS Simulator,id=$UDID" \
-only-testing "AppTests/SpecificTest" testCommon Mistakes
常见错误
| Mistake | Fix |
|---|---|
| "Unable to find destination" | Verify simulator exists with |
| "No scheme found" | Run |
| "App not found after build" | Use |
| Simulator not responding | Try |
| Build succeeds but app crashes | Check |
| Tests hang indefinitely | Add |
| Wrong simulator selected | Always use UDID from |
| Stale build artifacts | Use |
| 错误 | 解决方法 |
|---|---|
| "无法找到目标设备" | 使用 |
| "未找到scheme" | 运行 |
| "构建后未找到应用" | 使用 |
| 模拟器无响应 | 尝试执行 |
| 构建成功但应用崩溃 | 查看 |
| 测试无限期挂起 | 添加 |
| 选择了错误的模拟器 | 始终使用 |
| 构建产物过期 | 使用 |