尽管@3doubloons 提供了正确的答案,但我仍然想分享我的代码片段来演示这一点。
首先,LSEnvironment
在 Info.plist 中添加一个键。它的值是 env-var-names (作为键)及其字符串(只允许字符串!)值的字典。就我而言,我添加了LaunchUI
带有值的键"Yup"
然后,我的 main() 函数如下所示:
int main(int argc, const char * argv[]) {
int status = -1;
@autoreleasepool {
do {
NSProcessInfo *procInfo = [NSProcessInfo processInfo];
// deterine "UI" launch versus "command-line
id uiEnvVar = [[procInfo environment] objectForKey:@"LaunchUI"];
if([uiEnvVar isKindOfClass:[NSString class]] && [uiEnvVar isEqualToString:@"Yup"] ) {
status = NSApplicationMain(argc, argv); //launch app normally with UI
break;
}
id arguments = [procInfo arguments];
if (arguments == nil) {
printUsage();
break;
}
if ([arguments count] == 1 || [arguments containsObject:@"-h"] || [arguments containsObject:@"-help"]) {
printUsage();
status = 0;
break;
}
//process further arguments
if(processArgs(arguments) == NO) {
printUsage();
break;
}
// kick off protection.
if(YES != do_work()) {
break;
}
status = 0; //
// if your command-line should stay alive and handle events - you can do the following to block it from exiting.
[[NSRunLoop currentRunLoop] run];
} while(false);
}
return status;
}
我认为那种覆盖它。享受!