错误:无法读取构buildiOS Cordova时未定义的属性“replace”

我创build了一个使用cordova create project hello com.hello Hello

并添加iOS平台,使用cordova platform add iOS 。 并试图做cordova run ioscordova build ios cordova run ios cordova build ios

但它显示了我这个错误(我用–d / –verbose获取细节)。

/ usr / bin / codesign –force –sign – –timestamp = none /Volumes/Untitled/Plot/PlotReel/platforms/ios/build/emulator/PlotReel.app / Volumes / Untitled / Plot / PlotReel / platforms / ios /build/emulator/PlotReel.app:replace现有的签名

**build成成功**

没有find钩子“before_deploy”的脚本。 错误:TypeError:无法读取未定义的属性'replace'

 at remove (/Volumes/Untitled/Plot/test/platforms/ios/cordova/node_modules/ios-sim/src/lib.js:282:70) at Array.forEach (native) at Object.getdevicetypes (/Volumes/Untitled/Plot/test/platforms/ios/cordova/node_modules/ios-sim/src/lib.js:292:22) at Object.listEmulatorImages [as run] (/Volumes/Untitled/Plot/test/platforms/ios/cordova/lib/list-emulator-images:34:29) at deployToSim (/Volumes/Untitled/Plot/test/platforms/ios/cordova/lib/run.js:146:50) at /Volumes/Untitled/Plot/test/platforms/ios/cordova/lib/run.js:88:20 at _fulfilled (/Volumes/Untitled/Plot/test/platforms/ios/cordova/node_modules/q/q.js:834:54) at self.promiseDispatch.done (/Volumes/Untitled/Plot/test/platforms/ios/cordova/node_modules/q/q.js:863:30) at Promise.promise.promiseDispatch (/Volumes/Untitled/Plot/test/platforms/ios/cordova/node_modules/q/q.js:796:13) at /Volumes/Untitled/Plot/test/platforms/ios/cordova/node_modules/q/q.js:604:44 

我已经尝试卸载并再次安装cordova,但问题仍然存在。

请帮帮我。

新解决scheme

这个问题在最新版本的“ios-sim”软件包中得到修复(所以现在这可能是比较容易的解决scheme – 与以下列出的旧软件相比)。 为了将“ios-sim”包更新到最新版本,在你的terminal/ cmd中运行:

 cd platforms/ios/cordova/node_modules/ sudo npm install -g ios-sim@latest 

旧解决scheme

问题是,“ iPad Pro(12.9英寸) ”和“ iPad Pro(10.5英寸) ”的name_id_map[deviceName]返回undefined

你可以用console.log('name_id_map[ deviceName ]: ' + name_id_map[ deviceName ]);来检查它console.log('name_id_map[ deviceName ]: ' + name_id_map[ deviceName ]);

我通过添加一个if语句来解决这个bug,该语句检查设备是否在“ platforms / ios / cordova / node_modules / ios-sim / src / lib.js:282 ”中定义。

我replace了这个:

 list = []; var remove = function(runtime) { // remove "iOS" prefix in runtime, remove prefix "com.apple.CoreSimulator.SimDeviceType." in id list.push(util.format('%s, %s', name_id_map[ deviceName ].replace(/^com.apple.CoreSimulator.SimDeviceType./, ''), runtime.replace(/^iOS /, ''))); }; 

有了这个:

 list = []; var remove = function(runtime) { // remove "iOS" prefix in runtime, remove prefix "com.apple.CoreSimulator.SimDeviceType." in id if (name_id_map[deviceName] && runtime) { list.push(util.format('%s, %s', name_id_map[deviceName].replace(/^com.apple.CoreSimulator.SimDeviceType./, ''), runtime.replace(/^iOS /, ''))); } }; 

“iPad Pro(10.5英寸)”模拟器将不在列表中(但它不可能正常工作 – 没有检查)。

关于github的bug报告: https : //github.com/phonegap/ios-sim/issues/210

在你的项目文件夹根目录下,执行cd platforms/ios/cordova && npm install ios-sim

我有同样的错误。 对我来说,我将其追溯到platform / ios / cordova / node_modules / ios-sim / src / lib.js中的一个bug

 getdevicetypes: function(args) { ... list.devicetypes.forEach(function(device) { name_id_map[ filterDeviceName(device.name) ] = device.id; }); list = []; var remove = function(runtime) { // remove "iOS" prefix in runtime, remove prefix "com.apple.CoreSimulator.SimDeviceType." in id list.push(util.format('%s, %s', name_id_map[ deviceName ].replace(/^com.apple.CoreSimulator.SimDeviceType./, ''), runtime.replace(/^iOS /, ''))); }; 

在lib.js中总是发生“TypeError:Can not read property”replace为“undefined”的错误:289

 list.push(util.format('%s, %s', name_id_map[ deviceName ].replace(/^com.apple.CoreSimulator.SimDeviceType./, ''), runtime.replace(/^iOS /, ''))); 

所以我插入了一些debugging代码:

  list.devicetypes.forEach(function(device) { console.log('test 1 ' + device.name); console.log('test 2 ' + filterDeviceName(device.name)); name_id_map[ filterDeviceName(device.name) ] = device.id; }); 

这对我有效。 祝你好运。

  list = []; var remove = function(runtime) { // remove "iOS" prefix in runtime, remove prefix "com.apple.CoreSimulator.SimDeviceType." in id console.log('remove 1 ' + runtime); console.log('remove 2 ' + deviceName); console.log('remove 3 ' + name_id_map[ deviceName ]); list.push(util.format('%s, %s', name_id_map[ deviceName ].replace(/^com.apple.CoreSimulator.SimDeviceType./, ''), runtime.replace(/^iOS /, ''))); }; 

并得到以下输出:

 test 1 iPhone 5 test 2 iPhone 5 test 1 iPad Pro (9.7-inch) test 2 iPad Pro (9.7 inch) remove 1 iOS 10.2 remove 2 iPhone 5 remove 3 com.apple.CoreSimulator.SimDeviceType.iPhone-5 remove 1 iOS 10.2 remove 2 iPad Pro (9.7-inch) remove 3 undefined 

请注意filterDeviceName如何在填充散列的同时删除了减号字符。 当再次检索该值时,filter不适用,程序失败。

错误修复:在写入和读取哈希时应用filter。

  list.push(util.format('%s, %s', name_id_map[ filterDeviceName(deviceName) ].replace(/^com.apple.CoreSimulator.SimDeviceType./, ''), runtime.replace(/^iOS /, ''))); 

Github上有一个PR修复了我的问题: https : //github.com/phonegap/ios-sim/pull/213

只是在我的项目根目录中调用

 nano platforms/ios/cordova/node_modules/ios-sim/src/lib.js 

并添加了过滤设备名称的function,如下所示: https : //github.com/phonegap/ios-sim/pull/213/files

我最近上传到xcode 8.3.3 and ionic 3.4.0

我从myApp / platforms / ios / cordova / node_modules中删除了ios-sim目录,现在它正在工作。

另一种select是使用已经实施了ios-sim补丁的cordova-ios版本

cordova platform add https://github.com/apache/cordova-ios.git#4.4.0-ios-sim

请注意,这不是Apache Cordova的正式版本,它将包含在下一个4.4.1版本中。

我刚刚遇到这个,并认为我会添加一些对我npm install ios-sim东西 – 解决schemenpm install ios-sim没有。

我所做的只是打开XCode,它指向通用iOS设备,从我上次用它在物理设备上testing应用程序开始。 我只是将iOS模拟器更改为iOS模拟器列表中的任何内容,重试它,它就像一个魅力!

希望这可以帮助其他人在相同的情况。

使用npm install ios-sim@latest更新ios-sim版本并不适合我。 但是,如果在Github上find了一个好的和简单的解决scheme。

  1. 打开/platforms/ios/cordova/node_modules/ios-sim/src/lib.js
  2. 用你的代码编辑器searchdeviceName
  3. name_id_map[ deviceName ]replace为name_id_map[filterDeviceName(deviceName)]

你可以在这里findGithub的post

我运行以下命令,它解决了我的问题:

cd project_dir

sudo npm安装ios-sim @ latest