xcrun simctl createは、デバイスタイプとランタイムを指定してiOSシミュレータデバイスを新規作成するサブコマンドである。
基本的な使い方
create <name> <device type id> [<runtime id>]の形式で実行する。
$ xcrun simctl create "TIL Test" "iPhone 16"
No runtime specified, using 'iOS 26.5 (26.5 - 23F77) - com.apple.CoreSimulator.SimRuntime.iOS-26-5'
83F5C3A7-879C-4228-99E7-E0466DB93C68
ランタイムを省略すると、指定したデバイスタイプと互換性のある最新のランタイムが自動的に選択される。 標準出力には作成したデバイスのUUIDのみが出力され、選択されたランタイムの情報は標準エラー出力に表示される。
作成したデバイスはsimctl list devicesで確認できる。
$ xcrun simctl list devices | grep "TIL Test"
TIL Test (83F5C3A7-879C-4228-99E7-E0466DB93C68) (Shutdown)
デバイスタイプ・ランタイムの指定方法
<device type id>と<runtime id>には、表示名(iPhone 16)と識別子(com.apple.CoreSimulator.SimDeviceType.iPhone-16)のどちらも指定できる。
利用可能な値は、それぞれsimctl list devicetypesとsimctl list runtimesで確認する。
$ xcrun simctl list devicetypes | head -3
== Device Types ==
iPhone 17 Pro (com.apple.CoreSimulator.SimDeviceType.iPhone-17-Pro)
iPhone 17 Pro Max (com.apple.CoreSimulator.SimDeviceType.iPhone-17-Pro-Max)
$ xcrun simctl list runtimes
== Runtimes ==
iOS 17.5 (17.5 - 21F79) - com.apple.CoreSimulator.SimRuntime.iOS-17-5
iOS 18.3 (18.3.1 - 22D8075) - com.apple.CoreSimulator.SimRuntime.iOS-18-3
iOS 26.0 (26.0 - 23A343) - com.apple.CoreSimulator.SimRuntime.iOS-26-0
...
ランタイムを明示的に指定してデバイスを作成する例である。
$ xcrun simctl create "TIL Test iOS17.5" "iPhone 15" "com.apple.CoreSimulator.SimRuntime.iOS-17-5"
886E68FD-D541-4DED-B4FD-FC560617726E
ヘルプ記載の短縮形式はiOSでは通らない
ヘルプの<runtime id>の説明には、watchOS3やwatchOS 3.2のような短縮形式の例が記載されている。
<runtime id> A valid and available runtime. Find these by running "xcrun simctl list runtimes".
If no runtime is specified the newest runtime compatible with the device type is chosen.
Examples: ("watchOS3", "watchOS3.2", "watchOS 3.2", "com.apple.CoreSimulator.SimRuntime.watchOS-3-2",
"/Volumes/path/to/Runtimes/watchOS 3.2.simruntime")
この例に倣いiOS 18.3やiOS18.3のような短縮形式をiOSランタイムに対して試したところ、いずれもInvalid runtimeエラーになった。
$ xcrun simctl create "TIL Test ShortRuntime" "iPhone 15" "iOS 18.3"
Invalid runtime: iOS 18.3
$ xcrun simctl create "TIL Test ShortRuntime2" "iPhone 15" "iOS18.3"
Invalid runtime: iOS18.3
ヘルプの例はwatchOS向けに書かれたものであり、iOSランタイムに同じ書き方が通用するとは限らない。
確実に指定するには、simctl list runtimesの出力に表示される識別子(com.apple.CoreSimulator.SimRuntime.iOS-18-3)をそのまま使うのがよい。
非互換な組み合わせを指定するとエラーになる
デバイスタイプとランタイムの組み合わせによっては、互換性がなくデバイスを作成できない場合がある。
例えば、iOS 26系のデバイスタイプiPhone 17に対して古いランタイムiOS 17.5を指定すると、エラーになる。
$ xcrun simctl create "TIL Test Incompatible" "iPhone 17" "com.apple.CoreSimulator.SimRuntime.iOS-17-5"
An error was encountered processing the command (domain=com.apple.CoreSimulator.SimError, code=403):
Incompatible device
Unable to create a device for device type: iPhone 17 (com.apple.CoreSimulator.SimDeviceType.iPhone-17), runtime: iOS 17.5 (17.5 - 21F79) - com.apple.CoreSimulator.SimRuntime.iOS-17-5
iPhone 17はiOS 26系のみに対応したデバイスタイプで、古いiOSバージョンのランタイムでは動作しないため発生するエラーである。
同じ名前のデバイスを複数作成できる
createの<name>に既存デバイスと同じ名前を指定しても、エラーにはならず別デバイスとして作成される。
$ xcrun simctl create "TIL Test" "iPhone 15" "com.apple.CoreSimulator.SimRuntime.iOS-26-5"
938D4062-BF5D-41C1-B38B-7978433FEB42
$ xcrun simctl list devices | grep "TIL Test"
TIL Test (83F5C3A7-879C-4228-99E7-E0466DB93C68) (Shutdown)
TIL Test (938D4062-BF5D-41C1-B38B-7978433FEB42) (Shutdown)
デバイスはUUIDで一意に識別されるため、名前の重複はチェックされない。 名前だけを頼りにデバイスを操作するスクリプトを書く場合は、同名デバイスが複数存在しうる前提で対処する必要がある。
活用例: CIでテスト専用デバイスをその場で作成する
CI環境でUIテストを実行する際、既存デバイスの状態(インストール済みアプリや設定)に依存しないよう、テスト実行のたびに専用デバイスを作成して使い捨てる方法がある。
$ UDID=$(xcrun simctl create "CI Test $(date +%s)" "iPhone 16" "com.apple.CoreSimulator.SimRuntime.iOS-26-5")
$ xcrun simctl boot "$UDID"
# テストを実行する
$ xcrun simctl delete "$UDID"
デバイス名にタイムスタンプを含めておくと、並列実行時にも名前が衝突せず、ログ上でどのテスト実行のデバイスかを識別しやすくなる。
deleteは起動中のデバイスに対してもエラーにならず実行できるため、テスト終了後に明示的なshutdownを挟む必要はない。
参考: xcrun simctl deleteでiOSシミュレータデバイスを削除する
