在模拟器上运行
启动模拟器
初始化 React Native 项目后,您可以在新创建的项目目录中运行以下命令。
- npm
- Yarn
shell
npm run ios
shell
yarn ios
如果一切设置正确,您很快就会看到新应用在 iOS 模拟器中运行。
指定设备
您可以使用 --simulator
标志指定模拟器应运行的设备,后跟设备名称作为字符串。默认设备是 "iPhone 14"
。如果您希望在 iPhone SE(第三代)上运行应用,请运行以下命令
- npm
- Yarn
shell
npm run ios -- --simulator="iPhone SE (3rd generation)"
shell
yarn ios --simulator "iPhone SE (3rd generation)"
设备名称对应于 Xcode 中可用设备的列表。您可以通过在控制台中运行 xcrun simctl list devices
来查看可用设备。
指定设备版本
如果您安装了多个 iOS 版本,还需要指定其相应的版本。例如,要在 iPhone 14 Pro (16.0) 上运行应用,请运行以下命令
- npm
- Yarn
shell
npm run ios -- --simulator="iPhone 14 Pro (16.0)"
shell
yarn ios --simulator "iPhone 14 Pro (16.0)"
指定 UDID
您可以指定 xcrun simctl list devices
命令返回的设备 UDID。例如,要使用 UDID AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA
运行应用,请运行以下命令
- npm
- Yarn
shell
npm run ios -- --udid="AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA"
shell
yarn ios --udid "AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA"