首页指南参考教程

iOS 模拟器

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


直接在计算机上开发应用比不断与 iPhone 或 iPad 交互更方便,尤其是当网络条件较慢或由于 LAN 限制而需要 隧道连接 时。

¥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.

1

安装 Xcode

¥Install Xcode

打开 Mac App Store,搜索 Xcode,然后单击“安装”(如果已有,则单击“更新”)。

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

如果你无法更新,那是因为你的操作系统可能已过时。如果你的 Xcode 版本已过时,你可能会遇到进一步的问题。例如,你可能无法将应用提交到 App Store。

¥If you're unable to update, it is because your operating system might be out of date. You may run into issues further down the line if your Xcode version is out of date. For example, you may not be able to submit your app to the App Store.

2

安装 Xcode 命令行工具

¥Install Xcode Command Line Tools

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

¥Open Xcode, choose Settings... from the Xcode menu (or press cmd ⌘ + ,). Go to the Locations and install the tools by selecting the most recent version in the Command Line Tools dropdown.

3

试试看

¥Try it out

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

¥Run your app with npx expo start and press i from the command line.

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

¥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.

如果故障排除提示没有帮助,请在我们的 Discord 和论坛、Stack Overflow 或 Google 中提问。

¥If the troubleshooting tips are not helpful, ask a question in our Discord and forums, Stack Overflow, or Google.

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

¥You can also press shift + i in the Expo CLI to interactively select a simulator to open.

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 的一些主要差异。不过,请参阅 苹果的文档 了解更多详细信息。

¥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:

  • 音频输入

    ¥Audio Input

  • 晴雨表

    ¥Barometer

  • 相机

    ¥Camera

  • 运动支持(加速计和陀螺仪)

    ¥Motion Support (accelerometer and gyroscope)

模拟器还会暂停 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 模拟器不响应打开命令。如果似乎卡在这个提示上,你可以手动打开 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 49 project
npx create-expo-app --template blank@49

# 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 终端 UI 中按 shift + i 并选择所需的模拟器来重新安装。

    ¥Manually uninstall Expo Go on your simulator and reinstall by pressing shift + i in the Expo CLI Terminal UI and selecting the desired simulator.

  • 如果这没有帮助,请聚焦模拟器窗口,然后在 Mac 工具栏中选择设备 > 擦除所有内容和设置...
    这将从空白图片重新初始化你的模拟器。这有时对于计算机内存不足且模拟器无法存储某些内部文件、使设备处于损坏状态的情况很有用。

    ¥If that doesn't help, focus the simulator window and in the Mac toolbar choose Device > Erase All Content and Settings...
    This will reinitialize your simulator from a blank image. This is sometimes useful for cases where your computer is low on memory and the simulator fails to store some internal files, leaving the device in a corrupt state.

Expo 中文网 - 粤ICP备13048890号