xcrun simctl locationには、位置情報のシナリオ一覧を表示するlistと、再生中のシナリオや設定済みの位置情報をクリアするclearという、状態を確認・リセットするための2つのオプションがある。

シナリオの一覧を表示する

listオプションで、xcrun simctl location runでiOSシミュレータにプリセットの移動シナリオを再生するrunで利用できるシナリオの一覧を表示できる。

$ xcrun simctl location booted list
Name                 Description
========================================================
City Run             City Run
City Bicycle Ride    City Bicycle Ride
Freeway Drive        Freeway Drive
Apple                Apple

起動していないデバイスに対するlistの挙動

実際に試したところ、シャットダウン状態のデバイスに対してlistを実行すると、エラーにはならずヘッダー行のみで中身が空のリストが返ってきた。

$ xcrun simctl location 1E84370D-0AE2-4AF1-9789-DE11A8D1AF77 list
Name                 Description
========================================================

シナリオの一覧はデバイスの状態に関わらず本来同じはずだが、今回試した1台(シャットダウン状態のiPhone 15 Pro)では取得できなかった。 起動中のデバイスに対して実行することを前提にした方がよい。

設定済みの位置情報をクリアする

clearオプションで、xcrun simctl location setでiOSシミュレータの位置情報を固定値に設定するsetで設定した位置情報をクリアできる。

$ xcrun simctl location booted set 35.681236,139.767125
$ xcrun simctl location booted clear

log streamlocationdのログを見ると、clear実行時に"Clearing all pending locations and settings"というメッセージが流れており、設定済みの位置情報がクリアされていることを確認できる。

$ xcrun simctl spawn booted log stream --level debug \
    --predicate 'eventMessage contains "Clearing all pending locations"'

再生中のシナリオ・移動を止める

clearrunで再生中のシナリオを止める役割も持つ。 実際にrunでシナリオを再生した状態でdeltaDistance(前回位置からの移動距離)のログを監視しながらclearを実行したところ、clearの実行後はdeltaDistanceのログが流れなくなり、再生が止まったことを確認できた。 xcrun simctl location startでiOSシミュレータに複数地点を経由する移動をシミュレートするstartによる移動についても同様の効果が期待できるが、今回start単体では検証していない。

$ xcrun simctl location booted run "City Run"
$ xcrun simctl location booted clear

起動していないデバイスに対するclearの挙動

listとは対照的に、シャットダウン状態のデバイスに対してclearを実行すると、実際にエラーになることを確認した。

$ xcrun simctl location 1E84370D-0AE2-4AF1-9789-DE11A8D1AF77 clear
An error was encountered processing the command (domain=com.apple.CoreSimulator.SimError, code=405):
Unable to lookup in current state: Shutdown

同じsimctl locationのサブオプションでも、今回試した1台ではlistが空の結果を返したのに対し、clearは明確にエラーを返すという違いがあった。

活用例: テスト間の状態リセット

位置情報を使ったテストを連続で実行する際、テストごとにclearを挟んでおくと、前のテストで設定した座標やシナリオの再生が次のテストに影響しない状態から始められる。

$ xcrun simctl location booted clear
$ xcrun simctl location booted set 35.681236,139.767125
# ここでテストを実行
$ xcrun simctl location booted clear

CIで複数の位置情報パターンをテストする場合、各テストの前後にclearを挟む習慣をつけておくと、意図しない座標が残ったまま次のテストが実行される事故を防げる。