iOS 模拟器

了解如何在 Mac 上安装 iOS 模拟器并使用它来开发应用。


直接在电脑上开发应用可能比不断与 iPhone 或 iPad 互动更方便,尤其是在网络状况较慢时,或由于局域网限制需要隧道连接时。

🌐 Developing your app directly on a computer can be more convenient than constantly interacting with an iPhone or iPad, especially when network conditions are slow or when a tunnel connection is required due to LAN limitations.

本指南讲解了如何在 Mac 上安装 iOS 模拟器以进行应用开发。请注意,iOS 模拟器只能安装在 macOS 上。如果你在 Windows 或 Linux 电脑上开发 iOS 应用,你将需要一台实际的 iOS 设备。

🌐 This guide explains how to install the iOS Simulator on your Mac for app development. Note that the iOS Simulator can only be installed on macOS. If you are developing an iOS app from a Windows or a Linux machine, you will need a physical iOS device.

设置 Xcode 和 Watchman

🌐 Setup Xcode and Watchman

1

安装 Xcode

🌐 Install Xcode

打开 Mac 应用商店,搜索 Xcode,然后点击 安装(如果已经安装则点击 更新)。

🌐 Open up the Mac App Store, search for Xcode, and click Install (or Update if you have it already).

2

安装 Xcode 命令行工具

🌐 Install Xcode Command Line Tools

打开 Xcode,从 Xcode 菜单中选择 设置...(或按 cmd ⌘ + ,)。转到 位置,在 命令行工具 下拉菜单中选择最新版本以安装工具。

3

在 Xcode 中安装 iOS 模拟器

🌐 Install an iOS Simulator in Xcode

要安装 iOS 模拟器,请打开 Xcode > 设置... > 组件,在 平台支持 > iOS ... 下,点击 获取

🌐 To install an iOS Simulator, open Xcode > Settings... > Components, and under Platform Support > iOS ..., click Get.

4

安装 Watchman

🌐 Install Watchman

Watchman 是一个用于监控文件系统变化的工具。安装它将带来更好的性能。你可以通过以下方式安装它:

Terminal
brew update
brew install watchman

4

试试

🌐 Try it out

使用 npx expo start 运行你的应用,然后从命令行按 i

你可能会收到一个关于需要接受 Xcode 许可的警告。运行它建议的命令。再次打开你的应用看看是否成功。如果没有,请查看下面的故障排除提示。

🌐 You may get a warning about needing to accept the Xcode license. Run the command that it suggests. Open your app again to see if it was successful. If not, check the troubleshooting tips below.

你也可以在 Expo CLI 中按下 Shift + i ,以交互方式选择要打开的模拟器。

Expo 轨道

🌐 Expo Orbit

你可以使用 Expo Orbit 应用,它允许你在 macOS 的菜单栏上一键启动构建和模拟器管理。

🌐 You can use the Expo Orbit app which allows launching builds and simulator management with one click from the menu bar on macOS.

使用 Expo 轨道

了解更多关于如何使用 Expo Orbit 的信息。

局限性

🌐 Limitations

虽然 iOS 模拟器对于快速开发非常有用,但它确实存在一些限制。下面我们将列出一些影响 Expo API 的主要区别。不过,更多详情请参阅 Apple 的文档

🌐 Although the iOS Simulator is great for rapid development, it does come with a few limitations. We'll list out a few of the main differences that affect Expo APIs below. However, see Apple's documentation for more details.

模拟器中以下硬件不可用:

🌐 The following hardware is unavailable in the Simulator:

  • 音频输入
  • 晴雨表
  • 相机
  • 运动支持(加速度计和陀螺仪)

模拟器还会在 iOS 11 及更高版本上暂停后台应用和进程。

🌐 The Simulator also suspends background apps and processes on iOS 11 and later.

故障排除

🌐 Troubleshooting

打开模拟器时,CLI似乎卡住了

🌐 The CLI seems to be stuck when opening a Simulator

有时 iOS 模拟器不会对 open 命令作出响应。如果它似乎卡在这个提示上,你可以手动打开 iOS 模拟器(open -a Simulator),然后在 macOS 工具栏中选择 文件 > 打开模拟器,并选择你想要打开的 iOS 版本和设备。

🌐 Sometimes the iOS Simulator doesn't respond to the open command. If it seems stuck on this prompt, you can open the iOS Simulator manually (open -a Simulator) and then in the macOS toolbar, choose File > Open Simulator, and select an iOS version and device that you'd like to open.

你可以使用此菜单打开模拟器的任何版本。你也可以同时打开多个模拟器,但 Expo CLI 将始终以最近打开的模拟器为目标。

🌐 You can use this menu to open any version of the simulator. You can also open multiple simulators at the same time, however, Expo CLI will always target the most recently opened simulator.

模拟器已打开,但 Expo Go 应用无法在其中启动

🌐 Simulator opened but the Expo Go app isn't opening inside of it

第一次在模拟器中安装应用时,iOS 会询问你是否想打开 Expo Go 应用。你可能需要与模拟器进行一些操作(点击、拖动某些东西)以显示此提示,然后按 确定

🌐 The first time you install the app in the simulator, iOS will ask if you'd like to open the Expo Go app. You may need to interact with the simulator (click around, drag something) for this prompt to show up, then press OK.

我如何强制更新到最新版本?

🌐 How do I force an update to the latest version?

创建一个具有所需 SDK 版本的项目,并在模拟器中打开它以安装特定版本的 Expo Go。

🌐 Create a project with the desired SDK version and open it in a simulator to install a particular version of Expo Go.

Terminal
# Bootstrap an SDK 53 project
npx create-expo-app --template blank@53

# Open the app on a simulator to install the required Expo Go app
npx expo start --ios

Expo CLI 正在打印关于 xcrun 的错误信息,我该怎么办?

🌐 Expo CLI is printing an error message about xcrun, what do I do?

对于各种错误,请尝试以下方法:

🌐 For miscellaneous errors, try the following:

  • 在模拟器上手动卸载 Expo Go,然后在 Expo CLI 终端界面中按 shift + i 重新安装,并选择所需的模拟器。
  • 如果这没有帮助,请将模拟器窗口置于前端,然后在 Mac 工具栏中选择 设备 > 清除所有内容和设置...
    这将从空白镜像重新初始化你的模拟器。这在电脑内存不足且模拟器无法存储一些内部文件,导致设备处于损坏状态的情况下有时非常有用。