Error: Could not find iPhone 6 simulator 靠谱的解决方法

使用场景

最新升级 Xcode 到最新版本 11.3,执行命令react-native run-ios会报错

Found Xcode workspace taroDemo.xcworkspace
Could not find iPhone 6 simulator

网上无效方法

首先,你需要:
node_modules /反应本地/本地CLI / runIOS / findMatchingSimulator.js
然后;
您需要更改此代码
if (!version.startsWith('iOS') && !version.startsWith('tvOS'))
如下
if (version.indexOf('iOS') !== 0 && !version.includes('iOS')) { continue; }

最终解决方案

先说解决办法
找到findMatchingSimulator.js
./node_modules/react-native/local-cli/runIOS/findMatchingSimulator.js

以下是修改后完整代码

function findMatchingSimulator(simulators, simulatorName) {
  if (!simulators.devices) {
    return null;
  }
  const devices = simulators.devices;
  var match;
  for (let version in devices) {

    console.log(" version in devices ");
    console.log(version);

    // Making sure the version of the simulator is an iOS or tvOS (Removes Apple Watch, etc)
    if (!version.startsWith('com.apple.CoreSimulator.SimRuntime.iOS') && !version.startsWith('com.apple.CoreSimulator.SimRuntime.tvOS')) {
      continue;
    }
    for (let i in devices[version]) {
      let simulator = devices[version][i];
      // Skipping non-available simulator
      if (simulator.isAvailable !== true) {
        continue;
      }
      let booted = simulator.state === 'Booted';
      if (booted && simulatorName === null) {
        return {
          udid: simulator.udid,
          name: simulator.name,
          booted,
          version
        };
      }
      if (simulator.name === simulatorName && !match) {
        match = {
          udid: simulator.udid,
          name: simulator.name,
          booted,
          version
        };
      }
      // Keeps track of the first available simulator for use if we can't find one above.
      if (simulatorName === null && !match) {
        match = {
          udid: simulator.udid,
          name: simulator.name,
          booted,
          version
        };
      }
    }
  }
  if (match) {
    return match;
  }
  return null;
}

module.exports = findMatchingSimulator;

分析

最关键的地方是添加下面两行代码

    console.log(" version in devices ");
    console.log(version);

添加后执行react-native run-ios,可以看到模拟器版本信息

productdeMacBook-Pro:taro-native-shell product$ react-native run-ios
Scanning folders for symlinks in /Users/product/Desktop/taro-native-shell/node_modules (15ms)
Found Xcode workspace taroDemo.xcworkspace
 version in devices 
com.apple.CoreSimulator.SimRuntime.iOS-9-2
 version in devices 
com.apple.CoreSimulator.SimRuntime.iOS-10-3
 version in devices 
com.apple.CoreSimulator.SimRuntime.tvOS-13-3
 version in devices 
com.apple.CoreSimulator.SimRuntime.iOS-13-3
 version in devices 
com.apple.CoreSimulator.SimRuntime.watchOS-6-1
 version in devices 
com.apple.CoreSimulator.SimRuntime.iOS-8-1
 version in devices 
com.apple.CoreSimulator.SimRuntime.iOS-9-3
 version in devices 
com.apple.CoreSimulator.SimRuntime.iOS-12-1
 version in devices 
com.apple.CoreSimulator.SimRuntime.iOS-10-0

以上可以看到模拟器的版本信息
敲黑板, 划重点
可以看到打印的信息,模拟器版本信息为
com.apple.CoreSimulator.SimRuntime.iOS-9-2
到这你肯定发现,上面修改代码的地方了

对的网上找的大部分都是
if (!version.startsWith('iOS') && !version.startsWith('tvOS')){ continue; }
改成
if (version.indexOf('iOS') !== 0 && !version.includes('iOS')) { continue; }

打印后你会发现每次 xcode 版本不同模拟器也会不一样,所以修改后还是无效,我的 xcode 是 11.3,打印信息如上,所以修改为
if (!version.startsWith('iOS') && !version.startsWith('tvOS')){ continue; }
改成
if (!version.startsWith('com.apple.CoreSimulator.SimRuntime.iOS') && !version.startsWith('com.apple.CoreSimulator.SimRuntime.tvOS')){ continue; }

还有一处判断方法也需要修改
simulator.availability !== '(available)'
改成
if (simulator.isAvailable !== true)

以上就是解决办法总结,也是网上找了很多办法,最后还是在GitHub Error: Could not find iPhone 6 simulator #21498 上找到的希望对大家有用吧,开发不易,希望大家都好好珍惜这段时光。

你可能感兴趣的:(Error: Could not find iPhone 6 simulator 靠谱的解决方法)