您如何等待应用程序在OS X中关闭? [英] How do you wait for an application to close in OS X?

查看:53
本文介绍了您如何等待应用程序在OS X中关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面的代码检查应用程序是否正在运行并关闭.有人可以提供一个示例,说明如何请求取消申请并等待其关闭才能继续吗?

I am using the code below to check if an application is running and close it. Can someone provide an example of how to request an application calose and wait for it to close before proceeding?

+ (BOOL)isApplicationRunningWithName:(NSString *)applicationName {
    BOOL isAppActive = NO;
    NSDictionary *aDictionary;
    NSArray *selectedApps = [[NSWorkspace sharedWorkspace] runningApplications];

    for (aDictionary in selectedApps) {
        if ([[aDictionary valueForKey:@"NSApplicationName"] isEqualToString: applicationName]) {
            isAppActive = YES;
            break;
        }
    }
    return isAppActive;
}

+ (void)stopApplication:(NSString *)pathToApplication {
    NSString *appPath = [[NSWorkspace sharedWorkspace] fullPathForApplication:pathToApplication];
    NSString *identifier = [[NSBundle bundleWithPath:appPath] bundleIdentifier];
    NSArray *selectedApps = [NSRunningApplication runningApplicationsWithBundleIdentifier:identifier];
    // quit all
    [selectedApps makeObjectsPerformSelector:@selector(terminate)];
}

推荐答案

您可以使用

You can use Key-Value Observing to observe the terminated property of each running application. This way, you'll get notified when each application terminates, without having to poll.

这篇关于您如何等待应用程序在OS X中关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆