Cordova:启动特定的 iOS 模拟器镜像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22310526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:40:49  来源:igfitidea点击:

Cordova: start specific iOS emulator image

iosiphonecordova

提问by EggMeister

I'm developing a cross-platform mobile app using Cordova, focussing mainly on iOS in the development stage.

我正在使用 Cordova 开发一个跨平台的移动应用程序,在开发阶段主要关注 iOS。

For my development process it would be ideal if I could start my Cordova app directly from the command line and have it load into a specified emulator. I can do this by running the following from the project root directory:

对于我的开发过程,如果我可以直接从命令行启动我的 Cordova 应用程序并将其加载到指定的模拟器中,那将是理想的。我可以通过从项目根目录运行以下命令来做到这一点:

$cordova run --debug --emulator iOS

This works fine, and results in an iOS-simulator running my app in a simulated iPhone 4 Retina with iOS 7.0.3

这工作正常,并导致 iOS 模拟器在带有 iOS 7.0.3 的模拟 iPhone 4 Retina 中运行我的应用程序

Besides this simulated device, I would also like to test on (for instance) an iPad. I have these emulation images installed, and I can start my app in them manually in Xcode. Also, the command list-emulator-images(located in project_dir/platforms/ios/cordova/lib) gives the following output:

除了这个模拟设备,我还想在(例如)iPad 上进行测试。我安装了这些仿真图像,我可以在 Xcode 中手动启动我的应用程序。此外,该命令list-emulator-images(位于project_dir/platforms/ios/cordova/lib)提供以下输出:

"iPhone Retina (3.5-inch)"
"iPhone Retina (4-inch)"
"iPhone Retina (4-inch 64-bit)"
"iPhone"
"iPad"
"iPad Retina"

However, the thing is: I can't seem to figure out how to start the emulator in anything other than the default (which appears to be the iPhone Retina (4-inch)emulation image). The relevant output of cordova helpgives the following information:

但是,问题是:我似乎无法弄清楚如何在默认情况下启动模拟器(这似乎是iPhone Retina (4-inch)模拟图像)。的相关输出cordova help提供以下信息:

run [--debug|--release]
    [--device|--emulator|--target=FOO]
    [PLATFORM] ............................ deploys app on specified platform devices / emulators

I've tried things like the following:

我试过如下:

cordova run --debug --emulator=iPad iOS

cordova run --debug --emulator=iPad iOS

And many variations thereof, but no luck. Every time it starts in the same emulator.

以及其中的许多变化,但没有运气。每次它在同一个模拟器中启动。

The documentationfor the command-line tool doesn't offer any information in this regard, and an extensive Google-search also failed to turn up anything. Am I missing something trivial? Or am I trying to do something weird? I really hope someone here has experience with this, and can provide some answers.

命令行工具的文档没有提供这方面的任何信息,广泛的 Google 搜索也没有找到任何信息。我错过了一些微不足道的东西吗?还是我想做一些奇怪的事情?我真的希望这里有人有这方面的经验,并且可以提供一些答案。

Thanks very much in advance!

首先十分感谢!

edit: forgot to mention explicitly; I'm doing all this on a Mac. As mentioned earlier, running the app in different emulators/simulators in Xcode works fine.

编辑:忘了明确提及;我在 Mac 上做这一切。如前所述,在 Xcode 中的不同模拟器/模拟器中运行应用程序工作正常。

回答by csantanapr

To find out what are the simulator images available you can use to list them

要找出可用的模拟器图像有哪些,您可以用来列出它们

$ cordova emulate ios --list
Available iOS Virtual Devices:
    iPhone-4s, 9.3
    iPhone-5, 9.3
    iPhone-5s, 9.3
    iPhone-6, 9.3
    iPhone-6-Plus, 9.3
    iPhone-6s, 9.3
    iPhone-6s-Plus, 9.3
    iPad-2, 9.3
    iPad-Retina, 9.3
    iPad-Air, 9.3
    iPad-Air-2, 9.3
    iPad-Pro, 9.3

Then use one of the simulator names in the --target parameter:

然后在 --target 参数中使用模拟器名称之一:

cordova emulate ios --target="iPhone-4s, 9.3"
cordova emulate ios --target="iPad-Air-2, 9.3"
cordova emulate ios --target="iPhone-6s, 9.3"
cordova emulate ios --target="iPhone-6-Plus, 9.3"

ImportantQuit the simulator before launching a different target simulator (On Menu bar select Simulator->Quit)

重要在启动不同的目标模拟器之前退出模拟器(在菜单栏选择Simulator->Quit

Take into account that you may need to quit the iOS simulator via menu to switch from 3.5 to 4 inch iPhone.

考虑到您可能需要通过菜单退出 iOS 模拟器才能从 3.5 英寸 iPhone 切换到 4 英寸 iPhone。

dynamic list is available in platforms/ios/cordova/lib/list-emulator-images

动态列表可在 platforms/ios/cordova/lib/list-emulator-images

回答by Ruslan Soldatenko

As say csantanapryou can use:

正如csantanapr所说,您可以使用:

cordova emulate ios --target="iPhone-4s"

but, in this case cordova (or PhoneGap or other) project will be launched on iPhone 4s simulator with iOS version 7.0.3.

但是,在这种情况下,cordova(或 PhoneGap 或其他)项目将在iOS 版本 7.0.3 的 iPhone 4s 模拟器上启动。

If you want launch project on same simulator, but with other version iOS (7.1 or 8.0, if it versions exist in your system)?

如果您想在同一个模拟器上启动项目,但使用其他版本的 iOS(7.1 或 8.0,如果它的版本存在于您的系统中)?

Of corse, you can do like say cobberboy:

当然,你可以说cobberboy

start a specific emulator and choose your ios version by directly using ios-sim.

启动一个特定的模拟器并直接使用 ios-sim 选择你的 ios 版本。

But you can improve --targetoption of cordova runcommand.

但是您可以改进--targetcordovarun命令的选项。

At first you must ensure what target iOS version available on your system.

首先,您必须确保系统上可用的目标 iOS 版本。

For it use answer of cobberboy:

为此,请使用cobberboy 的答案:

$ ios-sim showdevicetypes

Then you need to open the file your_project_dir/platforms/ios/cordova/lib/run.jsand find lines of code like below:

然后你需要打开文件your_project_dir/platforms/ios/cordova/lib/run.js并找到如下代码行:

// validate target device for ios-sim
// Valid values for "--target" (case sensitive):
var validTargets = ['iPhone-4s', 'iPhone-5', 'iPhone-5s', 'iPhone-6-Plus', 'iPhone-6',
    'iPad-2', 'iPad-Retina', 'iPad-Air', 'Resizable-iPhone', 'Resizable-iPad'];

For use iPhone-4s, 7.1(or some other) simple add it to array validTargets.

为了使用iPhone-4s, 7.1(或其他一些)简单地将它添加到 array validTargets

var validTargets = ['iPhone-4s', 'iPhone-4s, 7.1', 'iPhone-5', 'iPhone-5s', 'iPhone-6-Plus', 'iPhone-6',
    'iPad-2', 'iPad-Retina', 'iPad-Air', 'Resizable-iPhone', 'Resizable-iPad'];

And in

而在

cordova emulate ios --target="iPhone-4s, 7.1"

your --target="iPhone-4s, 7.1"will be valid.

您的--target="iPhone-4s, 7.1"遗嘱有效。

And function deployToSimof run.js:

而功能deployToSimrun.js

function deployToSim(appPath, target) {
// Select target device for emulator. Default is 'iPhone-6'
if (!target) {
    target = 'iPhone-6';
    console.log('No target specified for emulator. Deploying to ' + target + ' simulator');
}
var logPath = path.join(cordovaPath, 'console.log');
var simArgs = ['launch', appPath,
    '--devicetypeid', 'com.apple.CoreSimulator.SimDeviceType.' + target,
    // We need to redirect simulator output here to use cordova/log command
    // TODO: Is there any other way to get emulator's output to use in log command?
    '--stderr', logPath, '--stdout', logPath,
    '--exit'];
return spawn('ios-sim', simArgs);
}

convert iPhone-4s, 7.1to valid argument com.apple.CoreSimulator.SimDeviceType.iPhone-4s, 7.1for ios-sim.

转换iPhone-4s, 7.1到有效的论据com.apple.CoreSimulator.SimDeviceType.iPhone-4s, 7.1进行ios-sim

回答by cobberboy

TL;DR

TL; 博士

You can start a specific emulator and choose your ios version by directly using ios-sim.

您可以直接使用 ios-sim 启动特定的模拟器并选择您的 ios 版本。

export appname="./platforms/ios/build/emulator/Hello World.app"
ios-sim launch "$appname" --devicetypeid "com.apple.CoreSimulator.SimDeviceType.iPad-2, 8.0" --stderr ./platforms/ios/cordova/console.log --stdout ./platforms/ios/cordova/console.log

Details

细节

When I ran this:

当我运行这个时:

cordova emulate ios --target="iPad"

and looked at the processes running, I saw this (on a single line):

并查看正在运行的进程,我看到了这一点(在一行中):

ios-sim launch ./platforms/ios/build/emulator/HelloWorld.app 
        --stderr ./platforms/ios/cordova/console.log 
        --stdout ./platforms/ios/cordova/console.log 
        --family ipad 
        --exit

Investigating further into ios-sim, it looks like there are some more specific options, particularly:

进一步调查ios-sim,看起来有一些更具体的选项,特别是:

--devicetypeid <device type>    The id of the device type that should be simulated (Xcode6+). Use 'showdevicetypes' to list devices.
  e.g "com.apple.CoreSimulator.SimDeviceType.Resizable-iPhone6, 8.0"

So I did as it suggested and ran ios-sim with a "showdevicetypes" argument and got this:

所以我按照它的建议做了并使用“showdevicetypes”参数运行了ios-sim并得到了这个:

$ ios-sim showdevicetypes
com.apple.CoreSimulator.SimDeviceType.iPhone-4s, 7.1
com.apple.CoreSimulator.SimDeviceType.iPhone-5, 7.1
com.apple.CoreSimulator.SimDeviceType.iPhone-5s, 7.1
com.apple.CoreSimulator.SimDeviceType.iPad-2, 7.1
com.apple.CoreSimulator.SimDeviceType.iPad-Retina, 7.1
com.apple.CoreSimulator.SimDeviceType.iPad-Air, 7.1
com.apple.CoreSimulator.SimDeviceType.iPhone-4s, 8.0
com.apple.CoreSimulator.SimDeviceType.iPhone-5, 8.0
com.apple.CoreSimulator.SimDeviceType.iPhone-5s, 8.0
com.apple.CoreSimulator.SimDeviceType.iPhone-6-Plus, 8.0
com.apple.CoreSimulator.SimDeviceType.iPhone-6, 8.0
com.apple.CoreSimulator.SimDeviceType.iPad-2, 8.0
com.apple.CoreSimulator.SimDeviceType.iPad-Retina, 8.0
com.apple.CoreSimulator.SimDeviceType.iPad-Air, 8.0
com.apple.CoreSimulator.SimDeviceType.Resizable-iPhone, 8.0
com.apple.CoreSimulator.SimDeviceType.Resizable-iPad, 8.0

回答by Ariel Ibarra

Don't include version number

不包含版本号

cordova run ios --target="iPhone-6s"

回答by David Vezzani

As of Xcode 8.3.2...

从 Xcode 8.3.2 开始...

Old thread, I know, but it seems, perhaps, that the answer has changed slightly. The hints from earlier posts in this thread helped, but so did reading the documentation included in the code, <cordova-project>/platforms/ios/cordova/lib/run.js

我知道旧线程,但似乎答案略有变化。该线程中早期帖子的提示有所帮助,但阅读代码中包含的文档也有帮助,<cordova-project>/platforms/ios/cordova/lib/run.js

Execute ./platforms/ios/cordova/lib/list-emulator-imagesto list the available emulator images. Do not include the version number on the end when making the cordova call to run in the desired emulator.

执行./platforms/ios/cordova/lib/list-emulator-images以列出可用的模拟器图像。进行cordova 调用以在所需的模拟器中运行时,不要在末尾包含版本号。

cordova run ios --emulator --target="iPad-Air"

See more

查看更多

回答by Tino Rüb

fastest output of devices list: $ instruments -s devices

设备列表的最快输出: $ instruments -s devices

Just use the device name without the version.

只需使用没有版本的设备名称。

回答by Sergio

I can't comment on the answer above due to my low reputation, but the list of targets is available from:

由于我的声誉较低,我无法对上述答案发表评论,但目标列表可从以下位置获得:

start-emulator 

under

在下面

your platform/ios/cordova/lib/

Having said that, I cannot make the ipad retina emulator work...

话虽如此,我无法使 ipad 视网膜模拟器工作...

回答by Oleg Bondarenko

Runs iOS simulator with web request based on already generated build for cordova application. Execute this request from browser opens simulator on mac with iPhone 8Plus version: http://hostname:3000/cordova/build/[xxxx-buildnumber]/emulate?target=iPhone-8-Plus

基于已为cordova 应用程序生成的构建,使用Web 请求运行iOS 模拟器。使用 iPhone 8Plus 版本从浏览器打开模拟器执行此请求: http://hostname:3000/cordova/build/[xxxx-buildnumber]/emulate?target=iPhone-8-Plus

回答by BIRJA KUMAR

Different iphone and ipad simulator

不同的iphone和ipad模拟器

  1. cordova run ios --list

  2. cordova emulate ios --target "iPhone-7"

  1. 科尔多瓦运行 ios --list

  2. 科尔多瓦模拟 ios --target "iPhone-7"

回答by Black Mamba

@Birja's answer is working right now but the run command he used finally is still wrong so here the right answer:

@Birja 的答案现在有效,但他最终使用的运行命令仍然是错误的,所以这里是正确的答案:

To list all the devices available in simulator cordova run ios --list

列出模拟器中可用的所有设备 cordova run ios --list

It will result in something like this:

它会导致这样的事情:

Available ios devices:
Available ios virtual devices:
Apple-TV-1080p, tvOS 12.2
Apple-Watch-Series-2-38mm, watchOS 5.2
iPhone-5s, 12.2
iPhone-6, 12.2
iPad-Air-2, 12.2
iPad--5th-generation-, 12.2
iPad-Pro--9-7-inch-, 12.2
iPad-Pro, 12.2
iPad-Pro--12-9-inch---2nd-generation-, 12.2
iPad-Pro--10-5-inch-, 12.2
iPad--6th-generation-, 12.2
iPad-Pro--11-inch-, 12.2
iPad-Pro--12-9-inch---3rd-generation-, 12.2

cordova run ios --target "iPad-Pro, 12.2"Use any target from the above. To run in simulator.

cordova run ios --target "iPad-Pro, 12.2"使用上述任何目标。在模拟器中运行。