FindSimulator

概述

使用 simctl 查找 Xcode 模拟器的 UDID,以便在 xcodebuild 中用作“destination”参数。

获取与查询匹配的第一个可用模拟器的“destination”说明符。

指定要搜索“iOS”、“watchOS”或“tvOS”模拟器。 默认为“iOS”。 您还可以指定要定位的操作系统的主版本号和次版本号,或者指定“latest”以获取最新版本。

还有一个选项可以列出所有匹配项以及所有信息。 请注意,列表输出包含“id”和“name”的值。 作为 xcodebuild 的“destination”,它们是互斥的。

您还可以找到带有配对 Apple Watch 的 iPhone 模拟器。

如何使用

该工具不会创建任何文件。 它只是将其结果输出到标准输出。 您可以将结果存储在 shell 变量中

destination=$(findsimulator --os-type ios --major-os-version latest iPhone)
xcrun xcodebuild test -workspace MyApp.xcworkspace -scheme MyApp -destination "$destination"

例子

iOS 模拟器的第一个匹配项(默认),具有最新的操作系统版本(默认),并且名称中包含“iPhone 1”。

findsimulator "iPhone 1"
// platform=iOS Simulator,id=65213250-02E8-4FB2-8F75-F75C01430A57

列出所有 iOS 模拟器(默认),具有最新的操作系统版本(默认),并且名称中包含“iPhone 1”。

findsimulator -l "iPhone 1"
//platform=iOS Simulator,OS=15.4,id=65213250-02E8-4FB2-8F75-F75C01430A57,name=iPhone 13 mini
//...
//platform=iOS Simulator,OS=15.2,id=52B96F75-49C1-4EF0-BDD3-D205E448E468,name=iPhone 13 mini
//platform=iOS Simulator,OS=15.2,id=BB3CDAEF-EE05-463A-9D5E-D714F83D274D,name=iPhone 13 Pro Max
//platform=iOS Simulator,OS=15.2,id=1328ADB6-4229-4E27-A9E3-56FEFFFF0117,name=iPhone 13 Pro

如果没有“--list-all|-l”标志,则返回带有“-l”标志的结果的第一个匹配项。 这样你总是得到相同的模拟器,而没有“-l”标志。

正则表达式可以进一步描述设备。 例如,只列出以字符串“Pro”结尾的设备($ => 匹配结束)。 只有 Pro 设备,没有 Pro Max 设备

findsimulator -l -r "Pro$"
// platform=iOS Simulator,OS=17.5,id=D2E4C979-FFEA-4236-9F57-64C3209975A0,name=iPhone 15 Pro
// platform=iOS Simulator,OS=17.4,id=D135ADE1-6924-4D28-86F7-E11D5C03D60B,name=iPhone 15 Pro
// platform=iOS Simulator,OS=15.5,id=3A259DB0-3824-4AD1-B883-3250FF134CE3,name=iPhone 13 Pro

只列出以字符串“Pro”结尾的设备($ => 匹配结束),并且名称中包含字符串 13 或 23 或 33 等。

findsimulator -l -r "\d3\sPro$"
// platform=iOS Simulator,OS=15.5,id=3A259DB0-3824-4AD1-B883-3250FF134CE3,name=iPhone 13 Pro

您明白了吧...

带有 watchOS 8 和次版本“latest”的模拟器

findsimulator -o watchOS -m 8 -s latest --list-all
// platform=watchOS Simulator,OS=8.5,id=5E86FE91-B033-4675-A399-65D9BF8DE055,name=Apple Watch Series 7 - 45mm
// platform=watchOS Simulator,OS=8.5,id=20E3510B-4955-42E2-B986-27FC47D61389,name=Apple Watch Series 7 - 41mm
// platform=watchOS Simulator,OS=8.5,id=6825047D-B5E9-4DFE-AEB5-BC6ADE089404,name=Apple Watch Series 6 - 44mm
// platform=watchOS Simulator,OS=8.5,id=C549ADA0-F294-49A8-B63B-6335C5F4B75C,name=Apple Watch Series 6 - 40mm
// platform=watchOS Simulator,OS=8.5,id=B56A7F31-8597-4E9F-842B-9D1706B1AD63,name=Apple Watch Series 5 - 44mm
// platform=watchOS Simulator,OS=8.5,id=84E856B2-2B8D-46E3-B254-9EAED4AD36C3,name=Apple Watch Series 5 - 40mm

包含“Series 5”名称的最新 watchOS 模拟器

findsimulator -o watchOS "Series 5" --list-all
// platform=watchOS Simulator,OS=8.5,id=B56A7F31-8597-4E9F-842B-9D1706B1AD63,name=Apple Watch Series 5 - 44mm
// platform=watchOS Simulator,OS=8.5,id=84E856B2-2B8D-46E3-B254-9EAED4AD36C3,name=Apple Watch Series 5 - 40mm

如何获取

使用 homebrew

brew tap a7ex/homebrew-formulae
brew install findsimulator

从源代码构建

如何安装

使用 homebrew

brew tap a7ex/homebrew-formulae
brew install findsimulator

下载的二进制文件

假设 findsimulator 应用程序在您的桌面上……

打开一个终端窗口并运行此命令

cp ~/Desktop/findsimulator /usr/local/bin/

通过在终端中运行此命令来验证 findsimulator 是否在您的搜索路径中

findsimulator -h

您应该看到该工具像这样响应

OVERVIEW: Interface to simctl in order to get suitable strings for destinations for the xcodebuild command.

USAGE: findsimulator [--os-type <os-type>] [--regex-pattern <regex-pattern>] [--major-os-version <major-os-version>] [--sub-os-version <sub-os-version>] [--pairs ...] [--list-all ...] [--version ...] [<name-contains>]

ARGUMENTS:
  <name-contains>         A simple 'string contains' check on the name of the simulator. Use the [-r | --regex-pattern] option for more finegrained searches instead.

OPTIONS:
  -o, --os-type <os-type> The os type. It can be either 'ios', 'watchos' or 'tvos'. Does only apply without '--pairs' option. (default: ios)
  -r, --regex-pattern <regex-pattern>
                          A regex pattern to match the device name. Does only apply without '--pairs' option.
  -m, --major-os-version <major-os-version>
                          The major OS version. Can be something like '12' or '14', 'all' or 'latest', which is the latest installed major version. Does only apply without '--pairs' option. (default: all)
  -s, --sub-os-version <sub-os-version>
                          The minor OS version. Can be something like '2' or '4', 'all' or 'latest', which is the latest installed minor version of a given major version. Note, if 'majorOSVersion' is set to 'latest', then minor version will also be 'latest'. Does only apply
                          without '-pairs' option. (default: all)
  -p, --pairs             Find iPhone Simulator in available iPhone/Watch Pairs.
  -l, --list-all          List all available and matching simulators.
  -v, --version           Print version of this tool.
  -h, --help              Show help information.

现在 findsimulator 的副本已在您的搜索路径中,请将其从您的桌面上删除。

你准备好了! 🎉