14

Is there any way to quit the iOS Simulator via a command line script?

Background:

I am setting up a Continous Integration environment to enable iOS builds to compile and be tested automatically. As part of this I am running scripts using Apple's UI Automation tool within Instruments.

I've managed to automate the execution of the scripts on the iOS Simulator by running Instruments from the command line BUT now I now want to automate quitting of the Simulator.

I have tried some Apple Script similar to this post: How can I reset the iOS Simulator from the command line? but get the error "Access for assistive devices is disabled". Hopefully, there is a simpler way?

Community
  • 1
  • 1
MandyW
  • 1,117
  • 3
  • 14
  • 23

5 Answers5

32

For Xcode 7+ the command is killall Simulator.

Danyal Aytekin
  • 4,106
  • 3
  • 36
  • 44
22

killall "iOS Simulator" in the Terminal will close it.

Also, you can launch it with iphonesim to have more control over it, including modifying the source to your needs.

Professor Todd
  • 571
  • 2
  • 12
djromero
  • 19,551
  • 4
  • 71
  • 68
  • Thanks very much for this simple solution. Can you see any issues with just killing off all the process without a more graceful shutdown? If not, I'll go with this :-) – MandyW Nov 09 '11 at 12:01
  • It depends on what you want to do, but I don't see any major problem. Kill the processes, sleep a few seconds or monitor they are gone, do whatever you'll after that. – djromero Nov 09 '11 at 13:06
  • 2
    on mavericks with xcode 6.1, I had to do `killall "iOS Simulator"`. Thanks for the starting point :D – gempesaw Nov 21 '14 at 18:22
14

Try this:

osascript -e 'tell app "iOS Simulator" to quit'
Professor Todd
  • 571
  • 2
  • 12
4

Pulling some of the commands together for XCode 6:

killall "iOS Simulator" 

xcrun simctl list | grep Booted | awk -F "[()]" '{ for (i=2; i<NF; i+=2) print $i }' | grep '^[-A-Z0-9]*$' | xargs -I uuid xcrun simctl shutdown uuid

xcrun simctl list | awk -F "[()]" '{ for (i=2; i<NF; i+=2) print $i }' | grep '^[-A-Z0-9]*$' | xargs -I uuid xcrun simctl erase uuid

open /Applications/Xcode.app/Contents/Developer/Applications/iOS\ Simulator.app
max_
  • 24,076
  • 39
  • 122
  • 211
  • 1
    This saved my day (night too)...is it needed to open the simulator in the last command? In my case everything works great even without it (xcodebuild test starts the simulator automaticaly). – JakubKnejzlik Feb 20 '15 at 05:04
-1

Based on that script you could create it like this:

tell application "iPhone Simulator"
    activate
end tell

tell application "System Events"
    tell process "iPhone Simulator"
        tell menu bar 1
            tell menu bar item "iOS Simulator"
                tell menu "iOS Simulator"
                    click menu item "Quit iOS Simulator"
                end tell
            end tell
        end tell
    end tell
end tell

The assistive devices error would occur on this as well. To fix that you need to go to System settings, Universal access and check "Enable access for assistive devices"

Dennis Bliefernicht
  • 5,147
  • 1
  • 28
  • 24
  • Thanks very much for this script, just wondering if turning on "Enable access for assistive devices" compromises security on the machine? – MandyW Nov 09 '11 at 13:45
  • Well it seems to enable various GUI scripting methods as well as the possibility for an application to capture input (e.g. keylogging). I guess it depends on the trust towards the applications running on the machine. Disabling it might provide some basic protection against keyloggers etc. but I guess more sophisticated malware has its way around that anyway. – Dennis Bliefernicht Nov 09 '11 at 17:40