LoginSignup
35
35

More than 5 years have passed since last update.

simctlでiOSシミュレータを制御する

Posted at

xcode6から simctl というiOSシミュレータの制御ツールが添付されるようになった。
基本的な使い方をいくつかピックアップする。

場所

Xcode6の中にある。

/Application/Xcode6.app/Contents/Developer/usr/bin/simctl

以下ではsimctlまでのパスは通っている感じで書きます。長くなるので。
もしくは xcrun つけてると読み替えてください。

使い方

help

とりあえずhelpはこんな感じ。

Usage: simctl [--noxpc] [--set <set path>] <subcommand> ... | help [subcommand]
Command line utility to control the iOS Simulator

For subcommands that require a <device> argument, you may specify a device UDID
or the special "booted" string which will cause simctl to pick a booted device.
If multiple devices are booted when the "booted" device is selected, simctl
will choose one of them.

Subcommands:
        create        Create a new device.
        delete        Delete a device.
        erase         Erase a device's contents and settings.
        boot          Boot a device.
        shutdown      Shutdown a device.
        rename        Rename a device.
        getenv        Print an environment variable from a running device.
        openurl       Open a URL in a device.
        addphoto      Add a photo to the photo library of a device.
        install       Install an app on a device.
        uninstall     Uninstall an app from a device.
        launch        Launch an application by identifier on a device.
        spawn         Spawn a process on a device.
        list          List available devices, device types, or runtimes.
        notify_post   Post a darwin notification on a device.
        icloud_sync   Trigger iCloud sync on a device.
        help          Prints the usage for a given subcommand.

device一覧の確認

たぶん大抵の人がすでにdevice作成ずみだと思う(普通にGUIで起動したことがあると思うので)。
その作成済みのdevice一覧を以下のコマンドで確認する。

simctl list

以下の様な結果が返る

== Device Types ==
iPhone 4s (com.apple.CoreSimulator.SimDeviceType.iPhone-4s)
iPhone 5 (com.apple.CoreSimulator.SimDeviceType.iPhone-5)
iPhone 5s (com.apple.CoreSimulator.SimDeviceType.iPhone-5s)
iPhone 6 Plus (com.apple.CoreSimulator.SimDeviceType.iPhone-6-Plus)
iPhone 6 (com.apple.CoreSimulator.SimDeviceType.iPhone-6)
iPad 2 (com.apple.CoreSimulator.SimDeviceType.iPad-2)
iPad Retina (com.apple.CoreSimulator.SimDeviceType.iPad-Retina)
iPad Air (com.apple.CoreSimulator.SimDeviceType.iPad-Air)
Resizable iPhone (com.apple.CoreSimulator.SimDeviceType.Resizable-iPhone)
Resizable iPad (com.apple.CoreSimulator.SimDeviceType.Resizable-iPad)
== Runtimes ==
iOS 7.0 (7.0 - Unknown) (com.apple.CoreSimulator.SimRuntime.iOS-7-0) (unavailable, runtime path not found)
iOS 7.1 (7.1 - 11D167) (com.apple.CoreSimulator.SimRuntime.iOS-7-1)
iOS 8.1 (8.1 - 12B411) (com.apple.CoreSimulator.SimRuntime.iOS-8-1)
== Devices ==
-- iOS 7.0 --
-- iOS 7.1 --
    iPhone 5 (B89F7F18-CEF4-4E9F-BC1C-F1EF6596BB06) (Shutdown)
-- iOS 8.1 --
    iPhone 4s (01AD2AC7-30E1-4951-8B08-38A75F527512) (Shutdown)
    iPhone 5 (B64C3404-099D-4063-9CD0-326DA84DB246) (Shutdown)
    iPhone 5s (679E25C2-8F92-4B31-883B-E536D3298747) (Shutdown)
    iPhone 6 Plus (02C2DBC7-796E-4D1C-8013-8178B98BB048) (Booted)
    iPhone 6 (D9AC24FE-BD54-4B59-8FC3-9112587F1943) (Shutdown)
    iPad 2 (2E7D1073-15C0-4E51-9346-26686E236FAB) (Shutdown)
    iPad Retina (A4A00337-73C4-49C4-ACD5-2DD13B6E984F) (Shutdown)
    iPad Air (8F2F56FF-F1E2-4260-9493-D65F00F42C91) (Shutdown)
    Resizable iPhone (11087367-4A47-41FB-9037-33591BC4BF1B) (Shutdown)
    Resizable iPad (11F5B456-6847-41B1-880E-423B3D4A6641) (Shutdown)

上記例では一個だけBoot中(iOS 8.1 iPhone 6 plus のdevice)
あと unavailable, runtime path not found となっているのはよくわからないけど、たぶんXcodeアップデートする前のが残っているのだと思う。iOS7.0 simulator入れてないし。

起動

bootサブコマンドにdeviceのUDIDを渡す。

simctl boot 02C2DBC7-796E-4D1C-8013-8178B98BB048

ただ上記で起動してもバックグラウンドで起動されるらしく、それをフォアグラウンドに表示する方法がわからなかった。
起動済みのデバイスを通常のiOS Simulatorで選択してもアタッチできるわけではなかった。

GUI付きで起動したい場合は simctl でbootするまえに以下のように起動する

open -a "iOS Simulator" --args -CurrentDeviceUDID 02C2DBC7-796E-4D1C-8013-8178B98BB048

シャットダウンは起動同様に以下のようにできる。

simctl shutdown 02C2DBC7-796E-4D1C-8013-8178B98BB048

アプリケーションのインストール・起動

インストールはappを指定する。シミュレータ向けにビルドしたものでないと動かないので注意。

simctl install 02C2DBC7-796E-4D1C-8013-8178B98BB048 sample.app

アンインストールはInfo.plistで指定した application id を指定する。

simctl uninstall 02C2DBC7-796E-4D1C-8013-8178B98BB048 "sample"

アプリケーションの起動も同様に application id を指定する。

simctl launch 02C2DBC7-796E-4D1C-8013-8178B98BB048 "sample"

その他サブコマンド

その他、デバイスの作成・削除・初期化なども可能。
指定したURLをシミュレータ内ブラウザで起動、写真をフォトライブラリに追加も可能。
シミュレータ内でログイン済みならiCloudの同期を行うことも可能らしい(試してない)。

spawn というサブコマンドもあり、シミュレータ上で任意のプロセスを実行できるっぽいのだが、ざっと調べた感じよくわからなかった。

参考

35
35
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
35
35