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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9490238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:28:37  来源:igfitidea点击:

Automatically close iOS Simulator when application is stopped in Xcode

iosxcodeios-simulator

提问by Matthew Walk

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.

每当应用程序在 Xcode 中停止时,是否可以关闭/退出 iOS 模拟器?我无法在 Xcode 或模拟器中找到设置。如果存在,它将有助于加快开发过程。

采纳答案by Mick MacCallum

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

保存此文件,然后打开 Xcode 首选项的行为部分,在运行完成部分将您的脚本文件添加到运行部分。希望这对你有用,但是这种方法似乎有点参差不齐,不幸的是,这是我能想出的最好方法!祝你好运! 在此处输入图片说明

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!

很遗憾您没有制作 OS X 应用程序,因为这样做非常容易。这部分无关紧要,但谁知道呢,您将来可能会使用它!

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


}

回答by jrturton

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.

您可以从 Xcode 停止调试会话并重新启动它,或者甚至只需从 Xcode 再次点击 cmd-r,新版本将在模拟器中正常运行。无需退出并重新启动模拟器。

回答by Gabriel Jensen

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

我在 xCode 5.1 中遇到了同样的问题。重新启动我的 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 不会让我再次运行,直到我退出模拟器,这很乏味。

回答by Jason McDermott

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

如果 Xcode 将这些简单的帮助程序作为默认设置,那就太好了,但至少我们可以自己手动添加它们。

回答by Darren

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.

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