我必须从我的 Cocoa 代码中以编程方式终止(而不是强制)应用程序。
实际上,这就是它的样子:
-(BOOL) terminateAppWithBundle:(NSString*)bundle {
NSArray* array = [NSRunningApplication runningApplicationsWithBundleIdentifier:bundle];
if ([array count] > 0){
NSRunningApplication* app = (NSRunningApplication*)[array objectAtIndex:0];
[array makeObjectsPerformSelector:@selector(terminate)];
float time = 0;
while (!app.isTerminated){
[NSThread sleepForTimeInterval:0.2];
time += 0.2;
if (time >= 15){
return NO;
}
}
}
return YES;
}
它工作得很好......但仅限于雪豹和狮子。
在 Leopard(我想支持)上,应用程序在启动时崩溃并显示以下错误消息:
Exception Type: EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000002, 0x0000000000000000
Crashed Thread: 0
Dyld Error Message:
Symbol not found: _OBJC_CLASS_$_NSRunningApplication
我想这是因为 NSRunningApplication 不是 10.5 SDK 的一部分......如果不使用该类,我怎么能做同样的事情?