Automatically close iOS Simulator when application

2020-03-24 05:47发布

问题:

Is it possible to have the iOS Simulator close/quit whenever an application is stopped in Xcode? I haven't been able to find a setting in Xcode or in Simulator to do so. It would help speed up the development process if it exists.

回答1:

To kill the simulator when your build is stopped, you will need to compile an executable file including the following

#!/bin/sh
osascript -e 'tell app "iPhone Simulator" to quit'

Save this file then open the behaviors section of Xcode preferences, in the run completes section add your script file to the run section. hopefully this will work for you, however this method seems to be a little spotty and is unfortunately the best way I've been able to come up with! Good luck!

It's too bad that you're not making a OS X app because then doing this is extremely easy. This part is irrelevant but who knows, you may be able to use it in the future!

- (IBAction)KillSim:(id)sender {

    NSLog (@"Sim Kill Begin");


    NSDictionary* errorDict;
    NSAppleEventDescriptor* returnDescriptor = NULL;

    NSAppleScript* scriptObject = [[NSAppleScript alloc] initWithSource:
                                   @"tell application \"iPhone Simulator\" to quit"];

    returnDescriptor = [scriptObject executeAndReturnError: &errorDict];
    [scriptObject release];

    if (returnDescriptor != NULL)
        {
            // successful execution
        if (kAENullEvent != [returnDescriptor descriptorType])
            {
                // script returned an AppleScript result
            if (cAEList == [returnDescriptor descriptorType])
                {
                    // result is a list of other descriptors
                }
            else
                {
                    // coerce the result to the appropriate ObjC type
                }
            } 
        }
    else
        {
            // no script result, handle error here
        }

    NSLog (@"Sim Killed End");


}


回答2:

I don't see how it'll speed up your dev time, however when using the simulator and you want to finish, just press cmd+q and it'll quit the simulator and automatically stop it in Xcode.



回答3:

You can stop the debug session from Xcode and restart it, or even just hit cmd-r again from Xcode and the new version will run fine in the simulator. There is no need to quit and re-start the simulator.



回答4:

I was having the same problem in xCode 5.1. Restarting my Mac solved it.

As for "Why does this speed up development", in my case Xcode wouldn't let me run again until I went and quit out of the emulator, which was tedious.



回答5:

You can add a 'run script' build phase to a new project (via Xcode project templates), by adding this to your TemplateInfo.plist;

<key>Targets</key>
<array>
<dict>
    <key>BuildPhases</key>
    <array>
        <dict>
            <key>Class</key>
            <string>ShellScript</string>
            <key>ShellPath</key>
            <string>/bin/sh</string>
            <key>ShellScript</key>
            <string>osascript -e 'tell app "iPhone Simulator" to quit'</string>
        </dict>
    </array>
</dict>

Alternatively, you can add this 'Run Script' to your Build Phases;

osascript -e 'tell app "iPhone Simulator" to quit'

In case you're interested, you can add another script to auto-increment your Build Number e.g.;

<key>Targets</key>
<array>
<dict>
    <key>BuildPhases</key>
    <array>
        <dict>
            <key>Class</key>
            <string>ShellScript</string>
            <key>ShellPath</key>
            <string>/bin/sh</string>
            <key>ShellScript</key>
            <string>
                buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")
                buildNumber=`echo $buildNumber +1|bc`
                /usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$INFOPLIST_FILE"
            </string>
        </dict>
        <dict>
            <key>Class</key>
            <string>ShellScript</string>
            <key>ShellPath</key>
            <string>/bin/sh</string>
            <key>ShellScript</key>
            <string>osascript -e 'tell app "iPhone Simulator" to quit'</string>
        </dict>
    </array>
</dict>

It would be great if Xcode had these simple helpers as default settings, but at least we can add them by hand ourselves.