有时我想阻止我的Cocoa应用程序启动,如何在init中停止它?

时间:2022-07-26 07:26:52

I just want to quit as fast as possible, before the nibs are loaded. I tried [NSApp stop:self] but that didn't seem to work. Is there a better way than getting my process and killing it?

我只想在加载笔尖之前尽快退出。我试过[NSApp停止:自我],但这似乎没有用。有没有比获得我的流程并杀死它更好的方法?

(I know it's a weird thing to do. It's for a good reason.)

(我知道这是一件很奇怪的事情。这是有充分理由的。)

4 个解决方案

#1


3  

Without knowing more, I'd say put the checking code into main() before invoking NSApplicationMain

在不知道更多的情况下,我会说在调用NSApplicationMain之前将检查代码放入main()

int main(int argc, char **argv)
{
    if(shouldExit() == YES)
    {
        exit(exitCode);
    }

    return NSApplicationMain(argc, (const char **) argv);
}

#2


3  

[[NSRunningApplication currentApplication] terminate];

Apple docs here. You can also use forceTerminate. You could also use nall's suggestion, but it will only work if you can do the work to check if the app needs to be terminated in main(). Otherwise, you'll need to do something more along the lines of what I suggested.

Apple文档在这里。您也可以使用forceTerminate。您也可以使用nall的建议,但只有在您可以完成工作以检查应用是否需要在main()中终止时,它才会起作用。否则,你需要按照我的建议做更多的事情。

#3


1  

If you can detect that you want to quit easily, modifying the main() function of your app is the "fastest" place:

如果您可以检测到您想要轻松退出,修改应用程序的main()函数是“最快”的地方:

int main(int argc, char **argv)
{

  id pool = [[NSAutoreleasePool alloc] init]; //needed if shouldExit() uses Cocoa frameworks

 @try {
    if(shouldExit())  {
      exit(0); //appropriate exit code, depending on whether this "fast" exit is normal or exceptional
    }
  }
  @finally {
    [pool drain];
  }

  return NSApplicationMain(argc, (const char **) argv);;
}

#4


0  

This should work:

这应该工作:

[[NSApplication sharedApplication] terminate: nil];

Reference

#1


3  

Without knowing more, I'd say put the checking code into main() before invoking NSApplicationMain

在不知道更多的情况下,我会说在调用NSApplicationMain之前将检查代码放入main()

int main(int argc, char **argv)
{
    if(shouldExit() == YES)
    {
        exit(exitCode);
    }

    return NSApplicationMain(argc, (const char **) argv);
}

#2


3  

[[NSRunningApplication currentApplication] terminate];

Apple docs here. You can also use forceTerminate. You could also use nall's suggestion, but it will only work if you can do the work to check if the app needs to be terminated in main(). Otherwise, you'll need to do something more along the lines of what I suggested.

Apple文档在这里。您也可以使用forceTerminate。您也可以使用nall的建议,但只有在您可以完成工作以检查应用是否需要在main()中终止时,它才会起作用。否则,你需要按照我的建议做更多的事情。

#3


1  

If you can detect that you want to quit easily, modifying the main() function of your app is the "fastest" place:

如果您可以检测到您想要轻松退出,修改应用程序的main()函数是“最快”的地方:

int main(int argc, char **argv)
{

  id pool = [[NSAutoreleasePool alloc] init]; //needed if shouldExit() uses Cocoa frameworks

 @try {
    if(shouldExit())  {
      exit(0); //appropriate exit code, depending on whether this "fast" exit is normal or exceptional
    }
  }
  @finally {
    [pool drain];
  }

  return NSApplicationMain(argc, (const char **) argv);;
}

#4


0  

This should work:

这应该工作:

[[NSApplication sharedApplication] terminate: nil];

Reference