简体   繁体   English

当应用程序在 Xcode 中停止时自动关闭 iOS 模拟器

[英]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?是否可以在 Xcode 中停止应用程序时关闭/退出 iOS 模拟器? I haven't been able to find a setting in Xcode or in Simulator to do so.我无法在 Xcode 或模拟器中找到设置。 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.保存此文件,然后打开 Xcode 首选项的行为部分,在运行完成部分将您的脚本文件添加到运行部分。 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.您没有制作 OS X 应用程序太糟糕了,因为这样做非常容易。 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.我在 xCode 5.1 中遇到了同样的问题。 Restarting my Mac solved it.重新启动我的 Mac 解决了它。

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.至于“为什么这会加快开发速度”,在我的情况下,Xcode 不会让我再次运行,直到我退出模拟器,这很乏味。

You can add a 'run script' build phase to a new project (via Xcode project templates), by adding this to your TemplateInfo.plist;您可以将“运行脚本”构建阶段添加到新项目(通过 Xcode 项目模板),方法是将其添加到您的 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.如果 Xcode 将这些简单的帮助程序作为默认设置,那就太好了,但至少我们可以自己手动添加它们。

In Xcode 12.3, simply just create simulator.sh (Shell Script) then fill with this code在Xcode 12.3中,只需创建simulator.sh(Shell Script),然后填写此代码

  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 > Behaviors 并设置为您的目标文件

XCode Preferences Image XCode 首选项图像

我不知道它会如何加快您的开发时间,但是当使用模拟器并且您想完成时,只需按 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.您可以从 Xcode 停止调试会话并重新启动它,或者甚至只需从 Xcode 再次点击 cmd-r,新版本将在模拟器中正常运行。 There is no need to quit and re-start the simulator.无需退出并重新启动模拟器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 XCode iOS模拟器 - 应用程序是像素化的 - XCode iOS simulator - application is pixelated 从Xcode关闭模拟器:在iOS上运行AppleScript - Close Simulator from Xcode: Running AppleScript on iOS Xcode 8消息模板应用程序:iOS模拟器上的错误 - Xcode 8 Messages Template Application: Error on iOS Simulator Xcode版本无法在ios模拟器上安装应用程序 - Xcode version unable to install the application on ios simulator 启动iOS模拟器时XCode锁定 - XCode Locks Up When Launching the iOS Simulator 尝试使用XCode 5在iPhone模拟器上运行iOS应用程序时不断出现断点错误 - Keep getting breakpoint error when trying to run iOS application on iphone simulator with xcode 5 运行/调试时,Xcode 4.5不会在模拟器或Mac应用程序中启动iOS应用程序 - Xcode 4.5 doesn't launch iOS application in simulator or mac app when running/debugging iOS-仅在直接在模拟器上运行,但在调试或通过Xcode运行时,应用程序才能正常运行 - iOS - Application crashes only when running directly on simulator but working fine while debugging or running through Xcode 如何在选择模拟器时在XCode中自动启用#define? - How can I enable a #define automatically in XCode when the simulator is selected? iOS XCode模拟器快照 - Snapshots for iOS XCode simulator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM