react-native 运行-ios 找不到任何模拟器

react-native run-ios can not find any simulator

我一直面临 'react-native run-ios' 无法启动的问题,无论我添加到 --simulator 参数的模拟器如何。 XCode 'command line tools'

的位置正确

我总是收到错误消息: 找不到 iPhone X 模拟器

Error: Could not find iPhone X simulator
    at resolve (/Users/eric/.../swim/node_modules/react-native/local-cli/runIOS/runIOS.js:149:13)
    at new Promise (<anonymous>)
    at runOnSimulator (/Users/eric/.../swim/node_modules/react-native/local-cli/runIOS/runIOS.js:134:10)
    at Object.runIOS [as func] (/Users/eric/.../swim/node_modules/react-native/local-cli/runIOS/runIOS.js:106:12)
    at Promise.resolve.then (/Users/eric/.../swim/node_modules/react-native/local-cli/cliEntry.js:117:22)

react-native 信息

>   React Native Environment Info:
>     System:
>       OS: macOS 10.14.2
>       CPU: (12) x64 Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
>       Memory: 6.76 GB / 32.00 GB
>       Shell: 3.2.57 - /bin/bash
>     Binaries:
>       Node: 10.15.0 - /usr/local/bin/node
>       Yarn: 1.13.0 - /usr/local/bin/yarn
>       npm: 6.4.1 - /usr/local/bin/npm
>     SDKs:
>       iOS SDK:
>         Platforms: iOS 12.1, macOS 10.14, tvOS 12.1, watchOS 5.1
>     IDEs:
>       Android Studio: 3.1 AI-173.4819257
>       Xcode: 10.1/10B61 - /usr/bin/xcodebuild
>     npmPackages:
>       react: 16.6.3 => 16.6.3 
>       react-native: 0.57.8 => 0.57.8 
>     npmGlobalPackages:
>       create-react-native-app: 1.0.0
>       react-native-cli: 2.0.1
>       react-native-git-upgrade: 0.2.7

我找到了一个临时修复方法:

在以下文件中:

/node_modules/react-native/local-cli/runIOS/findMatchingSimulator.js

...将第 42 行更改为:

if (!version.startsWith('com.apple.CoreSimulator.SimRuntime.iOS') && !version.startsWith('tvOS')) {

就这样

npm install

然后它会显示警告,然后使用以下命令修复它们

npm audit fix

更新

已在 v1.9.8 中修复。

更新 cli 是简单的解决方案

npm install -g react-native-cli@latest

此问题已修复但尚未发布。 https://github.com/react-native-community/react-native-cli/pull/274

所以修复下面的文件可以暂时解决。

/node_modules/react-native/local-cli/runIOS/findMatchingSimulator.js

更改此行

if (!version.startsWith('iOS') && !version.startsWith('tvOS')) {

if (!version.includes('iOS') && !version.includes('tvOS')) {

不要忘记在重新安装软件包时再次应用它。

从终端尝试此脚本

sed -i '' 's/startsWith/includes/g' node_modules/react-native/local-cli/runIOS/findMatchingSimulator.js

现在运行

react-native run-ios

我试过如下

打开 Xcode。然后 Preferences -> Select Components 在选项卡中。

然后安装列表中的任何一个(或多个)可用模拟器。最好是最近的置顶。

它解决了问题。

只需修改node_modules/react-native/local-cli/runIOS/findMatchingSimulator.js.

中的findMatchingSimulator.js即可

首先启动模拟器,然后 运行 xc运行 simctl list --json devices 并搜索 "state" : "Booted",.

然后在findMatchingSimulator函数的顶部添加:

return {
udid: <uuid from booted device>, // 'BFA2D7D0-AD49-472F-8279-585DDBBF9151'
name: <Name of the booted simulator>, //"iPhone X"
booted: true,
version: "com.apple.CoreSimulator.SimRuntime.iOS-13-1",

}

这里还有额外的临时修复。我正在使用 Catalina。

我这个文件:

/node_modules/react-native/local-cli/runIOS/findMatchingSimulator.js

将第 42 行更改为:

if (!version.includes('iOS') && !version.includes('tvOS')) {

将第 52-53 行更改为:

simulator.availability !== '(available)' &&
simulator.isAvailable !== 'YES'

至:

simulator.isAvailable !== true

因为从命令 xcrun simctl list --json devices 返回的 json 列表,属性 isAvailable 是布尔值并且没有 属性 'availability'.

我通过替换 YourProjectFolder/node_modules/react-native/local-cli/runIOS/findMatchingSimulator.js

中的第 62 行解决了这个问题

这一行

if (
        simulator.availability !== '(available)' &&
        simulator.isAvailable !== 'YES'
      ) {

有了这个

if (
      !simulator.isAvailable
   ) {

在某些情况下,每个人可能有一些不同的解决方案,只需通过放置 console.log() 来调试 findMatchingSimulator.js 文件,这样您就会知道为什么它没有检测到模拟器。在我的例子中,xcrun simctl list devices --json 命令不提供 simulator.availabilitysimulator.isAvailable 不是字符串,它是 bool。 我通过以下行打印了命令返回的模拟器列表

  console.log(JSON.stringify(simulator.simulatorName));
  console.log(simulator.isAvailable);
  console.log('-------------------------------------------------------------');

在 运行 命令 react-native run-ios --simulator="iPhone SE" 之后,我在终端中得到了输出,并根据控制台打印修复了代码。我的模拟器是 运行.

如果你不想用 npm audit fix 破坏你的应用程序,试试 npm install --force