Xcode运行低版本react-native

815 阅读2分钟

在开发过程中,我们在run项目时总会因为Xcode版本高,而项目较老的原因而无法运行,而Xcode因系统原因只能安装最新的版本,而新版本跑老项目时总会出现各种问题,以下列举几个问题和解决方案:

  1. xcode运行时遇到改错误时( 该错误为项目版本升级时的遗留问题,提供参考 ):
Cannot initialize a parameter of type 'NSArray<id<RCTBridgeModule>> *' with an rvalue of type 'NSArray<Class> *'

在项目中找到下面这个文件

node_modules/react-native/React/CxxBridge/RCTCxxBridge.mm

image.png

将644行处的代码

- (NSArray<RCTModuleData *> *)_initializeModules:(NSArray<id<RCTBridgeModule>> *)modules

替换为

(NSArray<RCTModuleData *> *)_initializeModules:(NSArray<id> *)modules
  1. 执行yarn ios时运行出现以下错误:
unable to find utility "instruments", not a developer tool or in PATH

这个错误是因为instruments在xcode13的版本中已经弃用并删除,官方给出另外一个命令来替代,此时项目版本为react-native版本为0.57.8,xocde版本为13.0

  • 解决方案参考:因版本问题文件位置可能不一样)

打开node_modules/react-native/local-cli/runIOS/runIOS.js文件,找到runIOS这个方法

image.png

这里就是run项目时instruments命令获取设备列表进行之后的操作

const devices = parseIOSDevicesList( child_process.execFileSync('xcrun', ['instruments', 's'], { encoding: 'utf8', }), );

因为instruments被弃用的原因,将此处修改为官方提供的另一个命令 xctrace

const devices = parseIOSDevicesList( child_process.execFileSync('xcrun', ['xctrace', 'list', 'devices'], { encoding: 'utf8', }), );

此外还需修改该同目录下的findMatchingSimulator.js文件,该文件是主要是处理模拟器器设备的状态,获取正在运行的设备、以及设备参数

function findMatchingSimulator(simulators, simulatorString) {

  if (!simulators.devices) {
    return null;
  }
  const devices = simulators.devices;

  const parsedSimulatorName = simulatorString ? simulatorString.match(/(.*)? (?:\((.*)?\))?/) : [];
  if (parsedSimulatorName[2] !== undefined) {
    var simulatorVersion = parsedSimulatorName[2];
    var simulatorName = parsedSimulatorName[1];
  } else {
    simulatorName = simulatorString;
  }

  var match;
  for (let version in devices) {
   
    // Making sure the version of the simulator is an iOS or tvOS (Removes Apple Watch, etc)
    if (!version.startsWith('iOS') && !version.startsWith('tvOS')) {
      continue;
    }
    if (simulatorVersion && !version.endsWith(simulatorVersion)) {
      continue;
    }
    for (let i in devices[version]) {
      let simulator = devices[version][i];
      // Skipping non-available simulator
      if (
        simulator.availability !== '(available)' &&
        simulator.isAvailable
      ) {
        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;
}

因为xctrace命令的原因,获取到的模拟器设备属性有些会没有,比如【availability】属性为undefined、,for in中的条件判断中可以运行yarn ios等命令调试,在你的版本react-native 0.57.8,xocde 13.0时可以修改以下几个地方:

  1. 判断设备是否属于IO和tvIOS, 可使用调试语句来查看设备型号等信息
const _version=version.substring(35,version.length)

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

      continue;

  }

2.注释掉 simulator.availability !== '(available)'判断,因为没有availability这个属性,该条件永远不成立

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

3.找到当前运行的模拟器,判断是否正在运行中,把booted加到条件判断中

 if (simulator.name === simulatorName && !match && booted) {
        match = {
          udid: simulator.udid,
          name: simulator.name,
          booted,
          version,
        };
 }
      

成功后运行 yarn ios 命令等待一段时间即可...原理上来说其它版本的rn同样能以这种方式来解决,jym自行尝试一下

参考链接

  • xcode 13发行说明

developer.apple.com/documentati…

  • CSDN 阿布

blog.csdn.net/cainiao1412…