反应本机更改默认iOS模拟器设备


158

当我运行此命令时:

react-native run-ios

我的应用默认在iPhone6模拟器设备中运行:

Found Xcode project RN.xcodeproj
Launching iPhone 6 (9.2)...

默认情况下,如何使该应用程序在其他模拟器设备(例如iPhone5s)中运行?


Xcode->产品->目标
zipzit

2
我不知道如何设置默认设备,但运行后react-native run-ios可以在中更改设备Simulator > Hardware > Device > iOS x.x > iPhone 5。这将一直有效,直到您退出模拟器。
Sealskej

Answers:


394

使用该--simulator标志指定模拟器。

这些是iOS 12.0及更高版本可用的设备:

npx react-native run-ios --simulator="iPhone 5s"
npx react-native run-ios --simulator="iPhone 6"
npx react-native run-ios --simulator="iPhone 6 Plus"
npx react-native run-ios --simulator="iPhone 6s"
npx react-native run-ios --simulator="iPhone 6s Plus"
npx react-native run-ios --simulator="iPhone 7"
npx react-native run-ios --simulator="iPhone 7 Plus"
npx react-native run-ios --simulator="iPhone 8"
npx react-native run-ios --simulator="iPhone 8 Plus"
npx react-native run-ios --simulator="iPhone 11"
npx react-native run-ios --simulator="iPhone 11 Pro"
npx react-native run-ios --simulator="iPhone 11 Pro Max"
npx react-native run-ios --simulator="iPad Pro (9.7-inch)"
npx react-native run-ios --simulator="iPad (7th generation)"
npx react-native run-ios --simulator="iPad Pro (11-inch)"
npx react-native run-ios --simulator="iPad Pro (12.9-inch)"
npx react-native run-ios --simulator="iPad Air (3rd generation)"
npx react-native run-ios --simulator="iPad Pro (11-inch) (1st generation)"

列出所有可用的iOS设备:

xcrun simctl list devices

当前无法设置默认值。

React Native Docs:在模拟器上运行


日Thnx了很多它帮助
Lakshaya Maheshwari

2
如果你想要的话 react-native run-ios --simulator="iPad Air"
Burak Tokak '17

22
要查看所有可用的设备,您可以在此处使用xcrun simctl list devices更多信息

1
万一您有多个具有相同名称的模拟器,例如ios 11的iPhone 6s,iOS 12的iPhone 6s等,则可以从Xcode->窗口->设备和模拟器
Chirag Purohit

为什么要使用多个“ iPad Pro”?
kojow7

48

您也可以通过在文件元素中添加一个条目来使用npm。例如scriptspackage.json

"launch-ios": "react-native run-ios --simulator \"iPad Air 2\""

然后使用这个: npm run launch-ios


3
恕我直言,这是最好的答案,因为它允许您将配置存储在代码/版本控制中。意味着您不需要记住命令行语法。
尼克(Nick)

1
我同意这个答案。您也可以使用单引号: "launch-ios": "react-native run-ios --simulator 'iPad Air 2'"
lfkwtz

24

如果您不了解,则有一个项目设置:

{project}/node_modules/react-native/local-cli/runIOS/runIOS.js

其中module.exports包括以下选项:

options: [{ command: '--simulator [string]', description: 'Explicitly set simulator to use', default: 'iPhone 7', }

我的是第231行,只需将其设置为已安装的有效模拟器即可运行 react-native run-ios,默认情况下将运行至该模拟器。


5
这实际工作,虽然文件被藏在里面(项目)/ node_modules /反应本地/本地CLI / runIOS /
马捷Ukmar

很好的解决方案。谢谢!
Ahmet Ardal

我实际上在节点模块文件夹下没有该模块。
Abdul Sadik Yalcin

1
是的,添加"runArguments": [ "--simulator", "iPhone 5s" ]到我的启动配置中效果很好。
CanPoyrazoğlu19年

22

您可以在~/.bash_profile文件中创建别名:

alias rn-ios="react-native run-ios --simulator \"iPhone 5s (10.0)\""

然后使用创建的别名运行react-native:

$ rn-ios


15

1)重命名您的模拟器,如果模拟器名称相同但iOS版本不同

Xcode -> Window -> Devices and Simulators -> Simulators.

在此处输入图片说明

2)打开您的本机项目文件夹

3)编辑package.json

"scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest",
    "flow": "node_modules/.bin/flow",
    "start-iphone6": "react-native run-ios --simulator \"iPhone 6 11.3\""
}

4) npm run start-iphone6


5

正如Ian L回答的那样,我还使用NPM来管理脚本。

例:

{
  "scripts": {
    "ios": "react-native run-ios --simulator=\"iPad Air 2\"",
    "devices": "xcrun simctl list devices"
  }
}

这样,我可以快速获得所需的信息:

  1. 列出所有设备: npm run devices
  2. 运行默认的模拟器: npm run ios

3

这是更改iOS模拟器的新途径,您只需更改

default: 'iPhone 6' or something else 

路径:

<project_root>/node_modules/@react-native-community/cli/build/commands/runIOS/runIOS.js


0

我在指定正确的iOS模拟器版本号的XCode 10.2中遇到问题,因此使用了:

react-native run-ios --simulator='iPhone X (com.apple.CoreSimulator.SimRuntime.iOS-12-1)'


0

使用此命令获取设备列表

xcrun simctl list devices

安慰

== Devices ==
-- iOS 13.5 --
    iPhone 6s (9981E5A5-48A8-4B48-B203-1C6E73243E83) (Shutdown) 
    iPhone 8 (FC540A6C-F374-4113-9E71-1291790C8C4C) (Shutting Down) 
    iPhone 8 Plus (CAC37462-D873-4EBB-9D71-7C6D0C915C12) (Shutdown) 
    iPhone 11 (347EFE28-9B41-4C1A-A4C3-D99B49300D8B) (Shutting Down) 
    iPhone 11 Pro (5AE964DC-201C-48C9-BFB5-4506E3A0018F) (Shutdown) 
    iPhone 11 Pro Max (48EE985A-39A6-426C-88A4-AA1E4AFA0133) (Shutdown) 
    iPhone SE (2nd generation) (48B78183-AFD7-4832-A80E-AF70844222BA) (Shutdown) 
    iPad Pro (9.7-inch) (2DEF27C4-6A18-4477-AC7F-FB31CCCB3960) (Shutdown) 
    iPad (7th generation) (36A4AF6B-1232-4BCB-B74F-226E025225E4) (Shutdown) 
    iPad Pro (11-inch) (2nd generation) (79391BD7-0E55-44C8-B1F9-AF92A1D57274) (Shutdown) 
    iPad Pro (12.9-inch) (4th generation) (ED90A31F-6B20-4A6B-9EE9-CF22C01E8793) (Shutdown) 
    iPad Air (3rd generation) (41AD1CF7-CB0D-4F18-AB1E-6F8B6261AD33) (Shutdown) 
-- tvOS 13.4 --
    Apple TV 4K (51925935-97F4-4242-902F-041F34A66B82) (Shutdown) 
-- watchOS 6.2 --
    Apple Watch Series 5 - 40mm (7C50F2E9-A52B-4E0D-8B81-A811FE995502) (Shutdown) 
    Apple Watch Series 5 - 44mm (F7D8C256-DC9F-4FDC-8E65-63275C222B87) (Shutdown) 

选择没有ID的模拟器字符串,这是一个示例。

iPad Pro(12.9英寸)(第4代)

最终命令

苹果手机

•iPhone 6s

react-native run-ios --simulator="iPhone 6s"

•iPhone 8

react-native run-ios --simulator="iPhone 8"

•iPhone 8 Plus

react-native run-ios --simulator="iPhone 8 Plus"

•iPhone 11

react-native run-ios --simulator="iPhone 11"

•iPhone 11专业版

react-native run-ios --simulator="iPhone 11 Pro"

•iPhone 11专业版Max

react-native run-ios --simulator="iPhone 11 Pro Max"

•iPhone SE(第二代)

react-native run-ios --simulator="iPhone SE (2nd generation)"

的iPad

•iPad Pro(9.7英寸)

react-native run-ios --simulator="iPad Pro (9.7-inch)"

•iPad(第7代)

react-native run-ios --simulator="iPad (7th generation)"

•iPad Pro(11英寸)(第二代)

react-native run-ios --simulator="iPad Pro (11-inch) (2nd generation)"

•iPad Pro(12.9英寸)第4代

react-native run-ios --simulator="iPad Pro (12.9-inch) (4th generation)"

•iPad Air(第三代)

react-native run-ios --simulator="iPad Air (3rd generation)"
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.