升级到Xcode 10后找不到iPhone 6模拟器

19

我升级到 Xcode 10 后,使用命令 react-native run-ios 无法在终端中运行模拟器,出现以下错误:

Could not find iPhone 6 simulator

当我进入 Xcode > 窗口(Window) > 设备和模拟器(Devices and Simulator) 页面时,模拟器是存在的,并且当我执行 xcrun simctl list devices 命令时也能看到模拟器列表。

我可以通过 react-native run-ios --device 命令将应用程序运行在我的手机上,并且可以通过 Xcode 运行。 我已经尝试了多个应用程序,因此不是应用程序的问题。

当我进入 Xcode > 首选项(Preferences) > 位置(Locations) 页面时,Xcode 10.0 (10A255) 被选中作为命令行工具(Command Line Tools)。

我尝试过重启电脑以及删除并重新安装 Xcode。

有任何想法吗?

这是我的设置:

  • MacOs High Sierra 10.13.6
  • Xcode 版本 10.0
  • react-native-cli: 2.0.1
  • react-native: 0.57.1

也许它正在寻找具有不同iOS版本的模拟器?当我尝试从命令行初始化时,也会发生相同的错误。 - Jose Vf
在我的情况下,它打开了一个 iPhone 6 iOS 8.1 模拟器,但是然后显示了一个与你的一样的错误信息。(“找不到 iPhone 6 模拟器”) - Jose Vf
@JoseVf:在我的电脑上,什么也没打开。我还尝试了以下方法:
  • 在Xcode > 窗口 > 设备和模拟器 中删除所有模拟器,只保留iPhone 6,并再次运行react-native run-ios
  • 添加一个新的模拟器并给它一个新的名称
  • 尝试使用react-native run-ios --simulator="iPhone 6"打开模拟器
但是都没有成功 :(
- Martin Leroux
@JoseVf:您升级到Xcode 10后也遇到了这个问题吗?您的MacOS版本是High Sierra 10.13.6吗? - Martin Leroux
Xcode 10和macOS Mojave(10.14),但也发生在High Sierra上。 - Jose Vf
12个回答

22

我打开了 node_modules/react-native/local-cli/runIOS/findMatchingSimulator.js 文件

然后我进行了以下替换:

if (version.indexOf('iOS') !== 0 )

替换成了

if (!version.includes("iOS" ))

同时

if (simulator.availability !== '(available)')

被替换成了

if (simulator.isAvailable !== true)


在RN 0.55.4中对我有用 - 我无法相信像Facebook这样的公司会发布这样的错误。 - chenop
@chenop 我和你使用的是同一个版本,但它对我来说不起作用。 - Darex1991
实际上,我已经升级到60版本了,这个问题已经解决了。 - samernady

18

我有类似的问题。 React Native - 0.52.3,XCode - 10.2 beta 2

function findMatchingSimulator(simulators, simulatorName) {
  if (!simulators.devices) {
    return null;
  }
  const devices = simulators.devices;
  var match;
  for (let version in devices) {
    // Making sure the version of the simulator is an iOS (Removes Apple Watch, etc)
    if (!version.includes('iOS')) {
      continue;
    }
    [...]
  }
}
  • 打开文件: node_modules/react-native/local-cli/runIOS/findMatchingSimulator.js

  • 检查您的设备版本是否正确,例如:在第29行中 console.log(version)

  • 将其与第30行中的条件进行比较:if (version.indexOf('iOS') !== 0) {

我有这样的版本列表:

com.apple.CoreSimulator.SimRuntime.watchOS-5-0 -1
com.apple.CoreSimulator.SimRuntime.tvOS-12-1 -1
com.apple.CoreSimulator.SimRuntime.tvOS-12-2 -1

在这种情况下,我的任何版本都不会返回true...如果你有的话

  • version.indexOf('iOS') !== 0替换为!version.includes('iOS')可以解决我的问题。

3
哇,这对我有用。我将version.start('iOS')更改为version.includes('iOS'),并解决了问题。令我惊讶的是,在Facebook的React Native团队中,一些问题居然能够通过质量保证。 - Eddie Eddie Eddie
这也是我的问题。 - Karthik T
没问题,谢谢。 - Roberto Rodriguez

10

这个问题已经在 react-native 中修复,所以你可以在 package.json 中更新这个包:

npm install -g npm-check-updates
ncu -u react-native
npm install

@chenop 它应该解决缺失的iOS模拟器问题,但是升级react-native可能会导致您的应用程序出现问题,因为您需要进行迁移。 - Danny Sullivan
2
告诉别人升级react-native是极为幼稚的建议,你用“可能会引起问题”的说辞轻描淡写地推开。 - Mike Walston
1
@MikeWalston 是的,诚然,这是一个非常天真的解决方案。 "可能会引起问题" 是我措辞不当。对于项目中核心软件的任何升级,您都需要权衡任何向后兼容性问题和错误修复的好处,在这种情况下,更好的逻辑定位iOS模拟器。 - Danny Sullivan

8

2
为了获得更好的回复质量,不仅要粘贴链接,还要简要解释一下该怎么做,然后再粘贴链接。如果链接失效,这个回复就会过时。 - Jose Vf
@JoseVf:已更新 ;) - Martin Leroux
1
这是 <你的React Native项目>/node_modules/react-native/local-cli/runIOS/findMatchingSimulator.js 的确切位置,以防有人不确定。 - user1791914
1
现在应该是 simulator.isAvailable !== true - Leo

5

我想和大家分享一下我是如何解决这个问题的,也许能帮到别人。

编辑此文件:node_modules/react-native/local-cli/runIOS/findMatchingSimulator.js

更改:

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

致:

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

这对我有用。

我使用console.log(version)来查看变量version得到的值,并且其值与预期不符。


3

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

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

替换这一行

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

使用

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

2
我有一份经过自我重构的findMatchingSimulator.js版本。如果将来有人仍然遇到此问题,而上述解决方案无法解决您的问题,只需覆盖...node_modules/react-native/local-cli/runIOS/findMatchingSimulator.js中的findMatchingSimulator-Method,并使用以下代码:
   function findMatchingSimulator(simulators, simulatorName) {
      if (!simulators.devices) {
        return null;
      }
      const devices = simulators.devices;
      var match = null;
      for(let version in devices){
          //Skip WatchOS
          if(!version.includes("tvOS") && !version.includes("iOS")){
              continue;
          }

          for(let d in devices[version]){
              var currentDevice = devices[version][d];
              if(!currentDevice.isAvailable){
                  continue;
              }
              if(currentDevice.state === "Booted" ){
                  let booted = currentDevice.state === "Booted";
                  if(currentDevice.name === simulatorName ){
                      return {
                              udid: currentDevice.udid,
                              name: currentDevice.name,
                              booted,
                              version,
                      };
                  }else if(!match){
                      //keep track of first available & bootable device
                      match = {
                         udid: currentDevice.udid,
                         name: currentDevice.name,
                         booted,
                         version,
                      };
                  }
              }
          }
      }
      if (match) {
        return match;
      }
      return null;
    }
   }

编辑:此代码将选择已经打开的模拟器,您仍然可以使用--simulator="NAME"参数。


2

使用react-native版本0.55.3XCode 10,我需要进行两个更改才能运行react-native run-ios

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

Original Answer翻译成"最初的回答"

  1. The change as described by @martin-leroux :

    Change

    // Skipping non-available simulator
    if (simulator.availability !== true) {
    

    to

    // Skipping non-available simulator
    if (
        simulator.availability !== '(available)' &&
        simulator.isAvailable !== 'YES'
    ) {`
    
  2. Change the line mentioned by @samernady :

    Change

    // Making sure the version of the simulator is an iOS or tvOS (Removes Apple Watch, etc)
    if (!version.startsWith('iOS') && !version.startsWith('tvOS')) {
    

    to

    // Making sure the version of the simulator is an iOS or tvOS (Removes Apple Watch, etc)
    if (!version.includes('iOS') && !version.startsWith('tvOS')) {
    

0

对我有效的一个选项

使用以下命令检查您的 Mac 上安装的模拟器列表

xcrun simctl list --json devices

在模拟器列表中选择任何一个模拟器名称,然后像这样运行。
react-native run-ios --simulator 'iPhone 6s'

希望这对你有用


0

前往 node_modules/react-native/local-cli/runIOS/,在 findMatchingSimulator.js 中将第 30 行替换为

if (version.indexOf('com.apple.CoreSimulator.SimRuntime.iOS') !== 0) {

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接