Search code examples
iosxcodeios-simulator

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.


Solution

  • 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! enter image description here

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