React Native Change默认iOS模拟器设备

时间:2016-05-19 16:21:01

标签: react-native

当我运行此命令时:

react-native run-ios

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

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

默认情况下,如何让应用程序在不同的模拟器设备(如iPhone5s)中运行?

15 个答案:

答案 0 :(得分:263)

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

这些是iOS 12.0的可用设备:

react-native run-ios --simulator="iPhone 5s"
react-native run-ios --simulator="iPhone 6"
react-native run-ios --simulator="iPhone 6 Plus"
react-native run-ios --simulator="iPhone 6s"
react-native run-ios --simulator="iPhone 6s Plus"
react-native run-ios --simulator="iPhone 7"
react-native run-ios --simulator="iPhone 7 Plus"
react-native run-ios --simulator="iPhone 8"
react-native run-ios --simulator="iPhone 8 Plus"
react-native run-ios --simulator="iPhone SE"
react-native run-ios --simulator="iPhone X"
react-native run-ios --simulator="iPhone XR"
react-native run-ios --simulator="iPhone XS"
react-native run-ios --simulator="iPhone XS Max"
react-native run-ios --simulator="iPad Air"
react-native run-ios --simulator="iPad Air 2"
react-native run-ios --simulator="iPad"
react-native run-ios --simulator="iPad Pro"
react-native run-ios --simulator="iPad Pro"
react-native run-ios --simulator="iPad Pro"
react-native run-ios --simulator="iPad Pro"
react-native run-ios --simulator="iPad"

列出所有可用的iOS设备:

xcrun simctl list devices

目前无法设置默认值。

React Native Docs: Running On Simulator

答案 1 :(得分:39)

您还可以通过向scripts文件的package.json元素添加条目来使用 npm 。 E.g。

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

然后使用它:npm run launch-ios

答案 2 :(得分:21)

您可以在version: '2' services: app1: image: 'mhart/alpine-node:6.3.0' container_name: app1 command: npm start app2: image: 'mhart/alpine-node:6.3.0' container_name: app2 command: npm start # databases [...] 文件中创建别名:

~/.bash_profile

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

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

答案 3 :(得分:20)

如果你追捕,有一个项目设置:

{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默认情况下它将运行到该模拟器。

答案 4 :(得分:9)

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

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

enter image description here

2)打开您的react native项目文件夹

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 :(得分:1)

我为它开发了CLI工具。您只需输入“rndcli”并选择设备

即可

https://github.com/nazrdogan/rndcli

答案 6 :(得分:1)

正如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

答案 7 :(得分:1)

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

default: 'iPhone 6' or something else 

路径:

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

答案 8 :(得分:1)

使用此命令获取设备列表

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

•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 Pro

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

•iPhone 11 Pro 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)"

•第4代iPad Pro(12.9英寸)

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

•iPad Air(第三代)

react-native run-ios --simulator="iPad Air (3rd generation)"

答案 9 :(得分:0)

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

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

答案 10 :(得分:0)

更改/node_modules/react-native/local-cli/runIOS/findMatchingSimulator.js的行代码

第55行

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import cm

np.random.seed(15)
df = pd.DataFrame(columns=['X', 'Y', 'val'])
df['X'] = np.random.randint(-226,227,300)
df['Y'] = np.random.randint(-27,462,300)
df['val'] = np.random.randint(0,3000,300)/1000
fig, ax = plt.subplots(figsize=(10,10))
hexbin = ax.hexbin(df['X'], df['Y'], C=df['val'].values, reduce_C_function=np.bincount)

替换:

  if (
    simulator.availability !== '(available)' &&
    simulator.isAvailable !== true
  ) {
    continue;
  }

答案 11 :(得分:0)

有多种方法可以实现这一目标:

  1. 使用--simulator标志
  2. 使用--udid标志

首先,您需要列出所有可用的设备。要列出所有运行的设备

xcrun simctl list device

这将给出如下输出:

这些是适用于iOS 13.0及更高版本的设备:

== Devices ==
-- iOS 13.6 --
    iPhone 8 (5C7EF61D-6080-4065-9C6C-B213634408F2) (Shutdown) 
    iPhone 8 Plus (5A694E28-EF4D-4CDD-85DD-640764CAA25B) (Shutdown) 
    iPhone 11 (D6820D3A-875F-4CE0-B907-DAA060F60440) (Shutdown) 
    iPhone 11 Pro (B452E7A1-F21C-430E-98F0-B02F0C1065E1) (Shutdown) 
    iPhone 11 Pro Max (94973B5E-D986-44B1-8A80-116D1C54665B) (Shutdown) 
    iPhone SE (2nd generation) (90953319-BF9A-4C6E-8AB1-594394AD26CE) (Booted) 
    iPad Pro (9.7-inch) (9247BC07-00DB-4673-A353-46184F0B244E) (Shutdown) 
    iPad (7th generation) (3D5B855D-9093-453B-81EB-B45B7DBF0ADF) (Shutdown) 
    iPad Pro (11-inch) (2nd generation) (B3AA4C36-BFB9-4ED8-BF5A-E37CA38394F8) (Shutdown) 
    iPad Pro (12.9-inch) (4th generation) (DBC7B524-9C75-4C61-A568-B94DA0A9BCC4) (Shutdown) 
    iPad Air (3rd generation) (03E3FE18-AB46-481E-80A0-D37383ADCC2C) (Shutdown) 
-- tvOS 13.4 --
    Apple TV (41579EEC-0E68-4D36-9F98-5822CD1A4104) (Shutdown) 
    Apple TV 4K (B168EF40-F2A4-4A91-B4B0-1F541201479B) (Shutdown) 
    Apple TV 4K (at 1080p) (D55F9086-A56E-4893-ACAD-579FB63C561E) (Shutdown) 
-- watchOS 6.2 --
    Apple Watch Series 4 - 40mm (D4BA8A57-F9C1-4F55-B3E0-6042BA7C4ED4) (Shutdown) 
    Apple Watch Series 4 - 44mm (65D5593D-29B9-42CD-9417-FFDBAE9AED87) (Shutdown) 
    Apple Watch Series 5 - 40mm (1B73F8CC-9ECB-4018-A212-EED508A68AE3) (Shutdown) 
    Apple Watch Series 5 - 44mm (5922489B-5CF9-42CD-ACB0-B11FAF88562F) (Shutdown) 

然后从输出中选择名称或uuid,然后根据需要进行操作。

  1. 要使用--simulator运行,请运行:
npx react-native run-ios --simulator="iPhone SE"
  1. 要使用--udid标志运行,请运行:
npx react-native run-ios --udid 90953319-BF9A-4C6E-8AB1-594394AD26CE

希望这个答案对您有所帮助。

答案 12 :(得分:0)

这是世博会!

更新 XCode 后,我总是得到 iPhone 12 Max 模拟器,但我更喜欢 iPhone 8。所以这里是我采取的步骤:

  1. 当模拟器处于对焦状态时,转到 File -> Open Simulator 并选择您希望作为默认设置的那个。
  2. 关闭旧模拟器,关闭 localhost:19002 并使用 ctr + C 停止终端中的服务器。
  3. 运行 npm start,然后按 i

现在它正在您打开的模拟器上运行。

快乐编码!

答案 13 :(得分:0)

iPhone Xʀ 模拟器

    "iosxr": "react-native run-ios --simulator=\"iPhone Xʀ\"",

只需将此添加到 package.json 文件中的脚本

这里使用的字母“ʀ”与“R”是不同的Unicode

然后点击 $ yarn iosxr 在 iPhone Xʀ Simulator 上启动应用程序

答案 14 :(得分:-1)

如果要更改默认设备,而只需要运行react-native run-ios,则可以在finder中搜索关键字“ runios”,然后打开文件夹并修复index.js文件,将“ iphone X”更改为需要的设备

[1]: https://i.stack.imgur.com/BCtR1.png