> ## Documentation Index
> Fetch the complete documentation index at: https://docs.devin.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Devin macOS 支持

> 在配备 Xcode 和 iOS 模拟器的 macOS 虚拟机上运行 Devin，构建、运行并测试 Apple 平台应用。

Devin 现已支持使用 macOS 虚拟机，因此可以构建和测试 iOS 与 macOS 应用程序，并[将构建产物上传到 TestFlight](https://docs.devin.ai/zh/onboard-devin/environment/testflight.md)。

![Devin 在 macOS 虚拟机上构建 iOS 游戏](https://mintcdn.com/cognitionai/1GViTZgEsjKKROrE/images/onboard-devin/macos-ios-app.gif?s=97a02d5b69dc53c55ce245b49d7e14af)

如需分步指引，请参阅[使用 Devin 构建 iOS 应用](https://docs.devin.ai/zh/tutorial-library/ios-app.md)。

> [!NOTE]
>
> 如果你使用的是 Dedicated SaaS 部署，请联系你的账户团队以启用 macOS 虚拟机。

## 工作原理

macOS 支持构建在与 Linux 相同的[声明式配置](https://docs.devin.ai/zh/onboard-devin/environment/blueprints.md)系统之上。蓝图中的 `runs-on` 字段用于告诉 Devin 在哪个平台上构建和运行，每个平台都有各自独立的快照。

与 Linux 的主要区别在于 shell、文件系统布局和包管理器：

| 方面 | Linux (默认) | macOS |
| - | - | - |
| 主目录 | `/home/ubuntu` | `/Users/devin` |
| 代码仓库目录 | `~/repos/<repo-name>` | `/Users/devin/repos/<repo-name>` |
| Shell | `bash` | `zsh` |
| 包管理器 | `apt-get` | `brew` (Homebrew 位于 `/opt/homebrew`) |
| 文件附件 | `/home/ubuntu/.files/` | `/Users/devin/.files/` |

## 启动 macOS 会话

你可以为每个会话单独选择 macOS：

* **蓝图**：添加 `runs-on: macos`，让该代码仓库的快照针对 macOS 构建 (见下文) 。
* **Slack**：使用 `!mac` [bang 命令](https://docs.devin.ai/zh/integrations/slack.md) 在 macOS 虚拟机上启动会话。
* **API**：在创建会话、计划或自动化任务时设置 `platform: "macos"`。参见 [API 参考](https://docs.devin.ai/zh/api-reference/overview.md)。
* **组织默认设置**：管理员可以在 **Settings → Devin → Default platform** 中将 macOS 设为新会话的默认平台，也可以通过 [Set Organization Default Platform](https://docs.devin.ai/zh/api-reference/v3/organization-settings/put-organization-default-platform.md) API 进行设置。如需查看当前值，可使用 [Get Organization Default Platform](https://docs.devin.ai/zh/api-reference/v3/organization-settings/get-organization-default-platform.md)。

## 编写 macOS 蓝图

### 单平台蓝图

如果你的代码仓库仅面向 Apple 平台，请在顶层使用 `runs-on: macos`：

```yaml theme={null}
runs-on: macos

initialize:
  - name: "Install build tooling"
    run: |
      brew install xcodegen swiftlint xcbeautify

maintenance: |
  xcodebuild -resolvePackageDependencies -project MyApp.xcodeproj -scheme MyApp

knowledge:
  - name: build
    contents: xcodebuild -scheme MyApp -destination 'platform=iOS Simulator,name=iPhone 17' build
  - name: test
    contents: xcodebuild -scheme MyApp -destination 'platform=iOS Simulator,name=iPhone 17' test
  - name: lint
    contents: swiftlint
```

### 多平台蓝图

若要为多个平台构建同一个代码仓库，请将每个平台写成独立的 YAML 文档，并以 `---` 分隔。每个文档各自声明自己的 `runs-on` 标签。有关该格式的背景信息，请参阅蓝图指南中的 [Multi-document YAML](https://docs.devin.ai/zh/onboard-devin/environment/blueprints.md#blueprint-sections) 提示框。

```yaml theme={null}
runs-on: default
initialize: |
  apt-get update && apt-get install -y build-essential

maintenance: |
  npm install

knowledge:
  - name: test
    contents: npm test

---
runs-on: macos
initialize: |
  brew install cocoapods

maintenance: |
  npm install
  (cd ios && pod install)

knowledge:
  - name: test
    contents: npm test
```

每个文档都会为其对应平台生成独立的快照构建。会话将从对应平台的快照启动。

> [!WARNING]
>
> 顶层 YAML 必须是 mapping，而不是序列。如果将上面的示例写成单个列表 (`- runs-on: default` / `- runs-on: macos`) ，后端会 reject。请使用上面所示的 `---` 分隔符。

## `runs-on` 字段

`runs-on` 字段映射到你账户中已注册的机器配置：

| 值 | 平台 |
| - | - |
| `default` 或 `linux` | Linux (默认平台) |
| `macos` | macOS |
| `windows` | Windows |

你可以将 `runs-on` 指定为字符串或列表：

```yaml theme={null}
# 单个平台
runs-on: macos

# 在一个块中指定多个平台（每个平台上运行相同的命令）
runs-on: [default, macos]
```

> [!WARNING]
>
> 列表语法会在列表中的每个平台上运行完全相同的命令。仅当命令确实跨平台时 (例如 `npm install`) 才使用这种写法。对于特定平台的命令 (如 Linux 上的 `apt-get` 或 macOS 上的 `brew`) ，请改用[多文档格式](#multi-platform-blueprint)。

## 用量与成本

macOS 会话目前消耗的用量与同等的 Linux 会话相同，不收取 macOS 附加费用。

> [!NOTE]
>
> 这是**促销发布定价**，可能会发生变化。

有关用量计量方式的详细信息，请参阅[用量](https://docs.devin.ai/zh/admin/billing/usage.md#macos-sessions)。

## 预装内容

macOS 会话镜像已预装 Apple 工具链，因此你的蓝图无需再自行下载：

| 类别 | 已包含 |
| - | - |
| Xcode | 默认为最新的 Xcode 26 正式版，位于 `/Applications/Xcode.app`，并同时提供 Xcode 27 预发布版 (例如 `/Applications/Xcode-27.0-RC.app`) |
| 模拟器 | 每个已安装的 Xcode 各配一个 iOS 模拟器运行时 (iOS 26 和 iOS 27) ，并预先配置好一台 iPhone 设备 |
| Apple 工具 | `xcodebuild`、`xcrun`、`simctl`、Swift 以及 Metal 工具链 |
| 包管理器 | 位于 `/opt/homebrew` 的 Homebrew |
| 语言 | Node.js、Python、Java、Rust (以及 `npm`、`yarn`、`pnpm`) |
| CLI 工具 | `git`、`git-lfs`、`gh`、`jq`、`ripgrep`、`ffmpeg`、`wget`、`direnv` |
| 浏览器 | Google Chrome |

随着 Apple 发布新版本、镜像随之更新，各组件版本也会变化。要确切了解某个会话中的版本情况，可让 Devin 运行：

```bash theme={null}
sw_vers
xcodebuild -version
ls -d /Applications/Xcode*.app
xcrun simctl list runtimes
```

### 选择 Xcode 版本

默认的 Xcode 即 `xcode-select` 所指向的版本。若只想在单条命令中使用其他已安装的版本，请设置 `DEVELOPER_DIR`：

```bash theme={null}
DEVELOPER_DIR=/Applications/Xcode-27.0-RC.app/Contents/Developer /usr/bin/xcodebuild -version
```

请使用 `/usr/bin/xcodebuild`(该 shim 会遵循 `DEVELOPER_DIR`),而不是通过 `PATH` 解析到的某个特定 Xcode 的 `Contents/Developer/usr/bin` 下的 `xcodebuild`,因为后者无论 `DEVELOPER_DIR` 如何设置,都只会报告自身的版本。

或者为整个会话切换默认版本:

```bash theme={null}
sudo xcode-select -s /Applications/Xcode-27.0-RC.app
```

在蓝图中写入你需要的版本，这样每个会话启动时都会使用正确的工具链。

## macOS 会话行为

### Shell

macOS 会话使用 **zsh** 作为默认 shell。大多数 POSIX shell 命令可直接沿用 Linux 蓝图中的写法，无需修改，但需注意 BSD 用户态工具的差异：`sed -i` 必须带一个参数 (`sed -i ''`) ，而 `gsed`、`gdate`、`greadlink` 等 GNU 工具需通过 Homebrew 的 `coreutils` formula 安装。

### 路径

```yaml theme={null}
# Linux 路径
- run: cp config.json ~/.config/myapp/config.json

# macOS 路径
- run: cp config.json /Users/devin/.config/myapp/config.json
```

仓库会被克隆到 `/Users/devin/repos/<repo-name>`，你上传到会话中的文件则会写入 `/Users/devin/.files/`。

### Secrets

[Secrets](https://docs.devin.ai/zh/product-guides/secrets.md) 在会话期间以环境变量的形式提供 (`$SECRET_NAME`) ，与 Linux 上的行为一致。可通过这种方式提供 App Store Connect API 密钥、签名凭据或私有 registry 令牌：

```yaml theme={null}
maintenance:
  - name: "Configure private Swift package registry"
    run: |
      git config --global url."https://$GIT_TOKEN@github.com/".insteadOf "https://github.com/"
```

关于 TestFlight upload 所需的 App Store Connect secrets 和 setup，参见[将 iOS build upload 到 TestFlight](https://docs.devin.ai/zh/onboard-devin/environment/testflight.md)。

### 会话休眠与唤醒

会话休眠时会将磁盘内容保存为快照。磁盘上的所有内容在唤醒后依然保留：已安装的工具、已克隆的代码仓库、构建缓存和派生数据。但正在运行的进程不会保留：开发服务器、模拟器和文件监听器需要在会话唤醒后重新启动。

### Computer Use

[Computer Use](https://docs.devin.ai/zh/work-with-devin/computer-use.md) 支持 macOS 会话：Devin 可获得一个完整的 macOS 桌面，配备 Chrome、鼠标和键盘，既能测试 macOS 原生应用，也能测试 Web 应用，并可将操作过程[录制](https://docs.devin.ai/zh/work-with-devin/testing-and-recordings.md)下来。在 macOS 上，Devin 使用 Command 键触发快捷键 (⌘C、⌘V、⌘Tab) ，而非 Control 键。

### iOS 模拟器

Devin 可以直接启动并操控 iOS 模拟器：

```bash theme={null}
open -a Simulator                  # 启动默认设备
xcrun simctl list devices          # 查看可用设备
xcrun simctl boot "iPhone 17"      # 启动指定设备
xcrun simctl install booted MyApp.app
xcrun simctl launch booted com.example.MyApp
```

会话 workspace 中的 **iOS Simulator** 选项卡会实时串流已启动的模拟器画面，让你可以实时观看 Devin 在你的 app 中逐步点击操作。这相当于 Apple 平台版的 [Android emulator support](https://docs.devin.ai/zh/onboard-devin/environment/android-emulation.md)。

## 使用技巧

### 预热构建缓存

冷构建 Xcode 会拖慢构建速度，影响开发体验。可使用 `environment.yml` 中的 `maintenance` 字段预热缓存。

```yaml theme={null}
runs-on: macos

initialize:
  - name: "Install tooling"
    run: brew install cocoapods xcodegen swiftlint

maintenance:
  - name: "Resolve dependencies and warm the build"
    run: |
      xcodebuild -resolvePackageDependencies -project MyApp.xcodeproj -scheme MyApp
      xcodebuild -scheme MyApp -destination 'platform=iOS Simulator,name=iPhone 17' build-for-testing
  - name: "Pre-boot the simulator"
    run: xcrun simctl boot "iPhone 17" || true
```

已解析的 Swift 软件包、CocoaPods 和 DerivedData 都会保留在快照中，因此新会话可以直接从增量构建开始。

### 网络访问

如果构建过程需要从 CocoaPods、Swift Package Manager、Firebase 或私有 registry 拉取依赖，就必须能访问这些主机。如果你的组织启用了受限的网络策略，请确保 macOS 允许列表中也包含 Linux 构建所使用的那些 registry。两者需要分别配置，一旦遗漏某个条目，通常会在构建中途表现为依赖解析失败或 TLS 失败。

### 运行容器

macOS 虚拟机不支持嵌套硬件虚拟化，因此容器运行时只能回退到 QEMU 的软件模拟 (TCG) 。Colima 会自动检测并切换到模拟模式：

```bash theme={null}
brew install colima docker qemu lima
colima start --vm-type qemu --arch aarch64 --cpu 4 --memory 8 --disk 30
```

虚拟机需要两到四分钟才能进入可用状态；首次启动时，模拟的客户机正在配置网络，等待 SSH 的过程可能会超时，如果 `colima start` 失败，重试即可。启动之后，container 在 CPU 上的运行速度约为原生的 1/15 到 1/25，每次启动还需几秒；镜像拉取则可跑满宿主机网络速度。用来跑 lint 或打包类 container 没什么问题，但拿来编译就很折磨了。如果工作中大量使用 container，建议改用 Linux 会话，或让 macOS 会话指向远程的 Docker 守护进程。

### 非必要不要在蓝图中安装 Xcode

Xcode 下载体积多达数 GB，而且 Apple 要求登录 Apple ID 才能下载。建议优先使用 image 中已有的版本，通过 `DEVELOPER_DIR` 或 `xcode-select` 来选择。如果确实需要其他版本或测试版，可以把 Apple ID 存为 [secret](https://docs.devin.ai/zh/product-guides/secrets.md)，让蓝图去下载该版本，但代价是构建会慢很多。

## 限制

| 限制 | 详情 |
| - | - |
| Docker 与容器 | 不支持嵌套硬件虚拟化，因此容器以软件模拟方式运行。参见[运行容器](#running-containers)。 |
| 物理设备 | 不支持 USB 直通，因此构建和测试均在模拟器上运行，而非真实的 iPhone 或 iPad。 |
| Xcode 下载 | 获取其他 Xcode 版本或模拟器运行时需提供 Apple ID 凭据，且下载耗时较长。 |
| 性能测量 | 在虚拟机中使用 Instruments 类工具进行计时与性能分析，无法反映真实设备的性能表现。 |

## 故障排查

**快照重建后的第一个会话中,构建明显变慢。** 这是因为 DerivedData 被从零重新构建了。可在 `maintenance` 中添加一次 `build-for-testing` 步骤,让快照带上已预热的构建产物。

**`xcodebuild` 选用了错误的工具链。** 检查 `xcode-select -p`,并在蓝图步骤中显式设置 `DEVELOPER_DIR`。

**找不到指定的 destination。** 运行 `xcrun simctl list devices available`,查看已安装的运行时实际提供了哪些设备,并让 `-destination` 中的名称和系统版本与之匹配。

**依赖解析卡住,或因 TLS 错误而失败。** 通常是该主机不在你的组织针对 macOS 的网络允许列表中。参见[网络访问](#network-access)。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.