tidev / ioslib

IOS Utilities Library
Other
68 stars 37 forks source link

How do you select which type of simulator to load? #54

Closed j2kun closed 7 years ago

j2kun commented 7 years ago

The docs are unclear on this.

cb1kenobi commented 7 years ago

ioslib.simulator.launch() expects either a "simHandle" or the simulator's UUID.

You can call ioslib.simulator.findSimulators() and pass in options like simType to have it find (or suggest) a simulator and it then fires the callback with the simHandle. Check out the JSDoc for more info: https://github.com/appcelerator/ioslib/blob/master/lib/simulator.js#L366-L385.

j2kun commented 7 years ago

This isn't very helpful. Most libraries I've tried allow you to pass something like "iPhone 7 (10.2)", the output of xcrun instruments -s devices, in such a way that doesn't depend on the machine you're running on. Can you post an example of how you would launch a simulator (maybe using findSimulators?) using such a string (or slightly reformated) as input?

cb1kenobi commented 7 years ago

ioslib doesn't support specifying a string like iPhone 7 (10.2) because the name is arbitrary. The name could be anything. You care about the sim runtime and device type. Unfortunately, you can't specify those when calling findSimulators(). It wouldn't hurt to add a runtime and device type criteria to findSimulators(), but until then, you must manually call ioslib.simulator.detect() and scan the simulators to find the one you want.

You can pass in a simType into findSimulators() with a value of "iphone" to filter out all iPad simulators. You can pass in a simVersion to get all sims for a specific iOS version.

At the of the day, there's no easy way to do what you want to do with ioslib. You will probably want to scan the results from detect() and pick the UUID for the exact simulator you want. This is exactly what I do when I use ioslib.

Just in case you're curious, here's where I call detect() first: https://github.com/appcelerator/titanium_mobile/blob/master/iphone/cli/commands/_build.js#L329. Next I call findSimulators(): https://github.com/appcelerator/titanium_mobile/blob/master/iphone/cli/commands/_build.js#L1868. After the build finishes, I call launch() here: https://github.com/appcelerator/titanium_mobile/blob/master/iphone/cli/hooks/run.js#L42. It's a bit complicated, but I'm working on that.

j2kun commented 7 years ago

the name is arbitrary

I would argue that the strings output by a first-party Apple tool are not arbitrary in this context. They are canon by virtue of Apple having a standardized tool that outputs those values.

But to each their own, I suppose.

cb1kenobi commented 7 years ago

I thought so too back when I first wrote ioslib, but it doesn't work that way. You can delete all the built-in simulators and create your own, then the name goes out the window. In fact, I did that and then had to write a script to recreate them:

const execSync = require('child_process').execSync;
const json = JSON.parse(execSync('xcrun simctl list --json'));

for (const runtime of Object.keys(json.devices)) {
    for (const device of json.devices[runtime]) {
        console.log(`Removing ${device.name} (${device.udid})`);
        execSync(`xcrun simctl delete ${device.udid}`);
    }
}

for (const deviceType of json.devicetypes) {
    for (const runtime of json.runtimes) {
        if (runtime.availability === '(available)') {
            console.log(`\nCreating ${deviceType.name} with ${runtime.name}`);
            try {
                execSync(`xcrun simctl create "${deviceType.name}" ${deviceType.identifier} ${runtime.identifier}`);
                console.log('SUCCESS!');
            } catch (e) {}
        }
    }
}

console.log();
console.log(execSync('xcrun simctl list').toString());

Referring to a simulator by UUID is the best way to identify a simulator.

j2kun commented 7 years ago

But but.... why would anyone delete all the built-in simulators and pick their own names? I mean I guess you did it, but using the built-in names would be a great default for the average user :)

cb1kenobi commented 7 years ago

Great question! I had 4 different Xcode versions installed and somehow I ended up with duplicate simulators and things were getting messed up when I was trying to figure out the Watch Simulator device pairs, especially when switching between CoreSimulator service versions. Fun times. :)