简体   繁体   中英

Automatically close iOS Simulator when application is stopped in Xcode

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.

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");


}

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.

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 eg;

<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.

In Xcode 12.3, simply just create simulator.sh (Shell Script) then fill with this code

  1. #!/bin/sh
  2. osascript -e 'tell app "Simulator" to quit'

after that, open XCode > Preferences > Behaviors and set to your target file

XCode Preferences Image

我不知道它会如何加快您的开发时间,但是当使用模拟器并且您想完成时,只需按 cmd+q 即可退出模拟器并在 Xcode 中自动停止它。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM