43

I created a cordova project using cordova create project hello com.hello Hello.

And added iOS platform using cordova platform add iOS. And tried to do cordova run ios after cordova build ios.

But it shows me this error(I used --d/ --verbose to get the details).

/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: replacing existing signature

** BUILD SUCCEEDED **

No scripts found for hook "before_deploy". Error: TypeError: Cannot read property 'replace' of undefined

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

I have tried uninstalling and installing cordova again, but the problem is still remaining.

Please help me.

Fenix
  • 1,552
  • 2
  • 23
  • 44
  • cordova -v shows 6.5.0 – Fenix Feb 20 '17 at 18:42
  • 1
    Have you tried running your command with the `-d`/`--verbose` option? Maybe you'll get some more information about your error... – Phonolog Feb 21 '17 at 16:39
  • I just updated my question with error details. Thanks. – Fenix Feb 21 '17 at 19:28
  • @Phoenix Try clearing your npm cache once and create the project onc again using the steps you followed. command to clear npm cache - "npm cache clean" Hope it works. Keep me posted – Gandhi Feb 23 '17 at 05:19
  • @Gandhi, Thank you, but it does not fix the issue, just tried it. – Fenix Feb 23 '17 at 07:31
  • @Phoenix In that case, the last option left out is to reinstall npm as npm modules seems to be corrupted in your case – Gandhi Feb 23 '17 at 07:43
  • I also tried it, will do it again. – Fenix Feb 23 '17 at 07:48
  • @Phoenix try running the following command - "cordova -d build ios" This might give some more info about the failure so that we can narrow down the issue – Gandhi Feb 23 '17 at 07:53
  • 1
    @Phoenix i m sorry. I overlooked at the question. Ok now i think i got the issue. Your issues seems to be with ios-sim command line utility. Ensure that you install the latest version of ios-sim. You cab check out this link for more info on an issue similar to this -https://github.com/phonegap/ios-sim/issues/203 This should hopefully resolve your issue – Gandhi Feb 23 '17 at 08:05
  • I tried to run iso-sim showdevicetypes and got the following. iPhone-5, 10.1 iPhone-5s, 10.1 iPhone-6, 10.1 iPhone-6s, 10.1 iPhone-6s-Plus, 10.1 iPhone-7, 10.1 iPhone-7-Plus, 10.1 iPhone-SE, 10.1 iPad-Retina, 10.1 iPad-Air, 10.1 iPad-Air-2, 10.1 iPad-Pro--9-7-inch-, 10.1 iPad-Pro, 10.1 Apple-TV-1080p, tvOS 10.0 Apple-Watch-38mm, watchOS 3.1 Apple-Watch-42mm, watchOS 3.1 Apple-Watch-Series-2-38mm, watchOS 3.1 Apple-Watch-Series-2-42mm, watchOS 3.1 – Fenix Feb 23 '17 at 08:30
  • I also reinstalled ios-sim using sudo npm install -g ios-sim, but it doesn't help me. – Fenix Feb 23 '17 at 08:35
  • Why dont you open your project with xCode ?? – Ryad Boubaker Feb 23 '17 at 10:10
  • It's also possible, i prefer working via terminal. – Fenix Feb 23 '17 at 10:31
  • @Phoenix What's your node and ios-sim version? i Still feel the link i sent earlier should have fixed your issue – Gandhi Feb 23 '17 at 10:51
  • @Phoenix it could also be due to some privilege issues as mentioned here - https://github.com/phonegap/ios-sim/issues/183 – Gandhi Feb 23 '17 at 11:47
  • I need definite answers, please add your answers, not comments. Thanks. – Fenix Feb 23 '17 at 17:33
  • 3
    @Phoenix You gotta understand that these kinds of issues are setup and environment specific. Not everyone will face these issues on daily basis. So it can be solved only via trail and error process. So definite answers are hard to come by and even if comes, it will be a guess. Moreover posting answers based on assumptions and getting it down-voted is even worse. If you dont want suggestions, thats fine. – Gandhi Feb 24 '17 at 05:19
  • The Old Solution works to me – Matheus Abreu Aug 30 '17 at 15:12

9 Answers9

89

New solution

This issue is fixed in the latest version of the "ios-sim" package (so now this is probably the easier solution - compared to the old one which is listed below). In order to update the "ios-sim" package to the latest version run this in your terminal/cmd:

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

Old solution

The problem is that the name_id_map[deviceName] returns undefined for "iPad Pro (12.9-inch)" and "iPad Pro (10.5-inch)".

You can check it with a console.log('name_id_map[ deviceName ]: ' + name_id_map[ deviceName ]);.

I fixed this bug by adding an if statements which checks if the device is defined in "platforms/ios/cordova/node_modules/ios-sim/src/lib.js:282".

I replaced this:

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 /, '')));
        };

with this:

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 /, '')));
            }
        };

The "iPad Pro (10.5-inch)" simulator won't be on the list (but it doesn't probably work anyway - didn't check).

Bug report on github: https://github.com/phonegap/ios-sim/issues/210

tedi
  • 6,350
  • 5
  • 52
  • 67
  • 1
    Thank you very much your last tip worked for me using with OnsenUI / Monaca – Hirbod Jun 13 '17 at 22:07
  • 2
    I can confirm, the following worked for me: $ cd platforms/ios/cordova/node_modules/ $ sudo npm install ios-sim@latest – Dallas Clark Jun 14 '17 at 12:35
  • See also my answer. Shazron Abdullah also posted that this has been fixed in the next release. More details how to add an "interims" platform to fix this problem https://github.com/phonegap/ios-sim/pull/213 – Hirbod Jun 24 '17 at 16:54
  • 1
    Thanks for your great solution. – Fenix Aug 03 '17 at 17:36
  • @JedatKinports why with `sudo`? For me works without `sudo` like a charm. – Petroff Aug 28 '17 at 09:51
  • @Petroff don't remember. If it works without sudo, fine. :) – tedi Aug 28 '17 at 10:04
  • I am sorry, but a `sudo` for a `npm install` for a local package. Please don't spread such tips. – Segers-Ian Aug 29 '17 at 16:53
  • I thought that i had the -g flag. Added it. But even then it's not needed, both should work, what ever you like. – tedi Aug 30 '17 at 06:40
37

in your project folder root, do cd platforms/ios/cordova && npm install ios-sim

Reza
  • 18,865
  • 13
  • 88
  • 163
15

I had the same error. For me I traced this down into a bug in platforms/ios/cordova/node_modules/ios-sim/src/lib.js

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 /, '')));
    };

The error always occured as "TypeError: Cannot read property 'replace' of undefined" in lib.js:289

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

So I inserted some debug code:

    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;
    });

This worked for me. Good luck.

    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 /, '')));
    };

and got the following output:

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

Notice how filterDeviceName removed the minus character while filling the hash. When the value is retrieved again, the filter is not applied and the program fails.

Bug fix: apply the filter while writing to and reading from the hash.

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

There is a PR on Github which fixed my problem: https://github.com/phonegap/ios-sim/pull/213

Just called following inside my project root

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

and added the function to filter the device name, as pointed out here: https://github.com/phonegap/ios-sim/pull/213/files

Hirbod
  • 607
  • 8
  • 28
3

I recently upgreaded to xcode 8.3.3 and ionic 3.4.0

I have removed ios-sim directory from myApp/platforms/ios/cordova/node_modules and now it's working.

chintan adatiya
  • 1,233
  • 14
  • 17
2

I have just run into this and thought I'd add something that worked for me - the solution npm install ios-sim didn't.

All I did was open up XCode and it was pointing to Generic iOS Device from when I last used it with testing an app on a physical device. I just changed the iOS Simulator to be anything on the iOS Simulator list, retried it and it worked like a charm!

Hope this might help somebody else in the same situation.

CYMR0
  • 566
  • 6
  • 16
1

Another option is to use a cordova-ios version with the ios-sim patch already implemented

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

Beware that this is not an official release of Apache Cordova, this will be included in next 4.4.1 version.

jcesarmobile
  • 51,328
  • 11
  • 132
  • 176
1

Updating the ios-sim version with npm install ios-sim@latest didn't worked for me. But if found a nice and easy solution on Github.

  1. Open /platforms/ios/cordova/node_modules/ios-sim/src/lib.js
  2. Search for deviceName with your code editor
  3. Replace name_id_map[ deviceName ] with name_id_map[filterDeviceName(deviceName)]

You can find the Github post here

Eyk Rehbein
  • 3,683
  • 3
  • 18
  • 39
0

I have run following commands and it solves my problem:

cd project_dir

sudo npm install ios-sim@latest

Deepak Bhatta
  • 474
  • 4
  • 16