Xcode 6 - Launch simulator from command line

Deadly 提交于 2019-11-26 21:14:31
Balaji Sekar

Found a way to achieve what I wanted.

Apple has introduced an interesting tool with Xcode 6!

simctl

simclt allows you to control the simulator that are running.

run xcrun simctl to get the list of available subcommands. Lots of new options to play around with.

Now to do what I wanted, here is the command to launch the simulator:

xcrun instruments -w "iPhone 5 (8.0 Simulator)"

-w is to provide a device type and to get the list of available devices.

Just execute this:

xcrun instruments -s

After launching the simulator you can control it using simctl

To install your app:

xcrun simctl install booted <app path>

To launch the app:

xcrun simctl launch booted <app identifier>

Hope this helps.

With Xcode 6, if you want to have the iOS Simulator.app boot a specific device when it launches, you can run this from the command line:

open -a "iOS Simulator" --args -CurrentDeviceUDID <DEVICE UDID>

where you can figure out the UDID of the device you want to boot from:

xcrun simctl list

With Xcode 7, the application was renamed Simulator.app, so you should update the above accordingly to:

open -a Simulator --args -CurrentDeviceUDID <DEVICE UDID>

For xcode 7:

open /Applications/Xcode.app/Contents/Developer/Applications/Simulator.app --args -CurrentDeviceUDID <DeviceUDID>

Get your simulator udid's from xcrun simctl list

To launch a simulator with a specific device booted I´m using list devices subcommand to get the list of all devices available

$ xcrun simctl list devices
== Devices ==
-- iOS 11.2 --
iPhone 5s (E3B6EA43-C805-49C2-9502-A44A465D8DF2) (Shutdown)
iPhone 6 (801E9E11-CA86-473A-9879-4B0742B827FB) (Shutdown)
iPhone 6 Plus (24013349-1A6F-489C-9A68-ABB00EBB4BBF) (Shutdown)
iPhone 6s (1A594D75-146C-4BEA-A250-1FADE7886114) (Shutdown)
iPhone 6s Plus (C2730FA0-11CB-49C9-A087-CB3C1BF1CC3D) (Shutdown)
iPhone 7 (F58B3749-3276-49E5-81C8-EBA1AEA7B242) (Shutdown)
iPhone 7 Plus (98167D8C-8F27-404C-AB02-588D9AAFD071) (Shutdown)
iPhone 8 (96322368-F763-4E0A-8576-ADE9F678211F) (Shutdown)
iPhone 8 Plus (E916D1EE-B67B-4C01-B3F5-C5C80CC4CDF8) (Shutdown)
iPhone SE (ABEFEDDF-7A7C-4B94-9E91-E065170FA47F) (Shutdown)
iPhone X (84DAB7AB-3CA2-4F5B-8C4E-A5B54CA15C31) (Shutdown)
iPad Air (DCD8CF4B-2C9F-4BA1-952A-ACB9CAD0A84D) (Shutdown)
iPad Air 2 (A47C9A05-233F-450F-9A39-318258E9ADEA) (Shutdown)
iPad (5th generation) (819C058E-64AC-4E73-8F41-2C0F919F8B56) (Booted)

this command will output a list of available devices with its UDIDs and statuses

Then I launch the simulator app specifying a device with the -CurrentDeviceUDID option

/Applications/Xcode.app/Contents/Developer/Applications/Simulator.app/Contents/MacOS/Simulator -CurrentDeviceUDID <DEVICE-UDID>

NOTE: replace the with a valid UDID from the list.

For example, if we want to launch the simulator with an Ipad (% generation booted):

/Applications/Xcode.app/Contents/Developer/Applications/Simulator.app/Contents/MacOS/Simulator -CurrentDeviceUDID 84DAB7AB-3CA2-4F5B-8C4E-A5B54CA15C31

UPDATE 23/05/2018

With Xcode 9.3 CurrentDevice UDID option is not working for me, as a workaround, I have to use simctl to boot the device in the simulator before open it.

xcrun simctl boot 2BF01FC0-7E29-4AF1-ADD1-886DF129A9A9
open -a Simulator 

You can create, erase, delete, boot, shutdown and upgrade simulators using simctl.

$ xcrun simctl create
Usage: simctl create <name> <device type id> <runtime id>

$ xcrun simctl delete
Usage: simctl delete <device> [... <device n>] | unavailable

To get the list of valid device types and runtimes

xcrun simctl list devicetypes

xcrun simctl list runtimes

You can specify the hardware and iOS version with -w flag. The format is

instruments -w "simulator-version"

For eg:

instruments -w "iPhone Retina (3.5-inch) - Simulator - iOS 7.1".

You will get the available hardvare-iOS combinations by using the instruments -w help command.

Verified in Xcode 9.4.1

Device list in a REALLY NICE FORMAT: ~/Library/Developer/CoreSimulator/Devices/device_set.plist

Root
    DefaultDevices
        com.apple.CoreSimulator.SimRuntime.iOS-8-4
             com.apple.CoreSimulator.SimDeviceType.iPad-Retina : AB335FAF-E3E3-4AE9-A0AF-D1C6AEB5FBD4

Then you want to run (open) your Simulator app and make sure that a new instance of the app is launched.

The app is here: /Applications/Xcode.app/Contents/Developer/Applications/Simulator.app

And the format you'd use is like this:

open -n -a <SimulatorPath> --args -CurrentDeviceUDID <SimDeviceTypeUDID>

So, if I wanted to launch the Simulator above, I'd do this.

open -n -a /Applications/Xcode.app/Contents/Developer/Applications/Simulator.app --args -CurrentDeviceUDID AB335FAF-E3E3-4AE9-A0AF-D1C6AEB5FBD4

Hot damn! I just tested it and it works on Mac OS 10.11.6. Now the next trick is telling the Simulator to open our installed app. I'm not there yet. Hope this helps. Thanks to https://stackoverflow.com/users/726106/jeremy-huddleston-sequoia for his detailed explanation.

To boot a simulator:

xcrun simctl boot "iPhone X"

It will boot as a headless mode. To make the simulator visible:

open -a Simulator

xcrun simctl boot is simpler than xcrun instruments -w. instruments requires the full device name.

Swapnil Mokashi

Run this command. The app will be restarted followed by changes. No need to rebuild it again.

ios-sim "launch" "/Library/WebServer/Documents/testapp/build/iphone/build/Debug-iphonesimulator/test.app" "--devicetypeid" "iPad-2" "--exit";

For devicetypeid list:

ios-sim showdevicetypes
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!