`react-native run-ios` returns Error: Could not find iPhone X simulator

六眼飞鱼酱① 提交于 2019-11-29 19:31:17
sdkcy

Edited: I copied your devices section of your output JSON and embedded to my own /node_modules/react-native/local-cli/runIOS/runIOS.js

function runOnSimulator(xcodeProject, args, scheme) {
return new Promise(resolve => {
    try {
        var simulators = {devices section of your json}; //Here
    } catch (e) {
        console.log("e", e);
        throw new Error('Could not parse the simulator list output');
    }

    const selectedSimulator = findMatchingSimulator(simulators, args.simulator);
    console.log("selected", selectedSimulator);
    if (!selectedSimulator) {
        throw new Error(`Could not find ${args.simulator} simulator`);
    }
    ...

And finally, it gave the same error as yours. So I figured out that parsing version of devices different. In your devices, version is;

"com.apple.CoreSimulator.SimRuntime.tvOS-12-1" //for tvOS
"com.apple.CoreSimulator.SimRuntime.iOS-12-1" // for iOS

but in react-native checks this version values like this(/node_modules/react-native/local-cli/runIOS/findMatchingSimulator.js);

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

So react-native can not recognize.
If we change this code with this;

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

the problem was solved. I checked that with your JSON data in my computer and it worked.

Weisheng Wu

To fix this error, simply reinstall project using npm

npm install

Then it will show a list of warnings, and to fix them use the following command:

npm audit fix

This should fix everything, and allow you to run iOS emulator react-native run-ios

Temporary fix:

Step 1: Open the file /node_modules/react-native/local-cli/runIOS/findMatchingSimulator.js

Step 2: Change a line of code, from this:

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

to this:

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

Step 3: Run react-native run-ios (you might need to run it twice, and don't forget to kill the Metro Bundler [the console that's running in the background] if it's running from your previous unsuccessful build!)

The problem was that after an update to Xcode - the simulator namespaces were added to the devices' versions list. The React Native build wasn't expecting these namespaces - hence the build break.

Open the file /node_modules/react-native/local-cli/runIOS/findMatchingSimulator.js

!version.startsWith('iOS') - > !version.includes('iOS')

after update to Xcode 11 xcrun returns true or false and not YES or NO as before.

goto /node_modules/react-native/local-cli/runIOS/findMatchingSimulator.js and in

find

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

and change it to

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

in my case i run the script: sed -i '' 's/startsWith/includes/g' node_modules/react-native/local-cli/runIOS/findMatchingSimulator.js and it worked. also suitable for people who can't update react-native

PROBLEM

There is no iPhone X!

Run Simulator by itself, in the top menu, look under Hardware, Device, iOS 13.0. You will see there is:

  • iPhone 8
  • iPhone 8 Plus
  • iPhone XS
  • iPhone XS Max
  • iPhone XR
  • ... and some iPads

When you execute run-ios, react-native is designed to match a requested device.

The internally hard coded default is iPhone X.

The function that tries to match the requested device is in:

/node_modules/@react-native-community/cli-platform-ios/build/commands/runIOS/findMatchingSimulator.js

This function is designed so you can give it a device, and an optional version number.

If the given device and version cannot be found, it will return a match using the first device in the list by default.

But... in reality, the first device is a watch, and any watch is excluded from matching, so this function will return null.

SOLUTION

Run Simulator first by itself, as described above, and make a note of which iPhone or iPad you want.

Then pass this name as an optional argument the the run-ios command line command as follows:

react-native run-ios --simulator="iPhone 8"

Also I answered here: React Native Issue #2328

My problem was that iPhone X simulator was not installed

You can check what simulators are installed with

 xcrun simctl list devices --json | grep iPhone

Then run

react-native run-ios --simulator="iPhone Xs"

I'm having the same issue each time I update Xcode.
I run this command:

sudo killall -9 com.apple.CoreSimulator.CoreSimulatorService

Then I run react-native run-ios again.

if could not found simulator still persists after replacing following lines of code in runSimulator.js.

Path for runSimulator.js -> /node_modules/react-native/local-cli/runIOS/findMatchingSimulator.js

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

with this one

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

comment following lines after replacing

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

It will launch iPhone simulator without any issue.

and 2nd approach after updating to XCode 11

Xcode 11 xcrun returns true or false and not YES or NO for isAvailable property as before. You can check that using the following command

xcrun simctl list devices --json 

above command will print all available devices like following

"com.apple.CoreSimulator.SimRuntime.iOS-12-2" : [
  {
    "state" : "Booted",
    "isAvailable" : true,
    "name" : "iPhone X",
    "udid" : "E53748D1-628B-4A99-A419-4D7AE7CE4745"
  }
]

Replace YES with true in following code

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

like this

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

With react-native-cli 2.0.1, you may want to just rename your simulator. By default Xcode will name some devices in the format <DeviceName> (<DeviceSize>). However this version of the CLI interprets anything in parentheses as the iOS version.

When trying to run a simulator named iPad Pro (10.5-inch), I was able to get it to run by simply renaming the simulator to iPad Pro [10.5-inch].

You can rename simulators by using the Simulator app's Hardware->Device->Manage Devices... menu, and right-clicking one of your simulators (in Xcode 10.2.1)

I faced the issue when I updated Xcode. All the simulators were not available/uninstalled. I am installing them again.

Just reboot your computer and run command again.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!