开发者

Sometimes I want to stop my Cocoa app from launching, how do I stop it in init?

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?

(I know it's a weird t开发者_如何学JAVAhing to do. It's for a good reason.)


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

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

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


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


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

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);;
}


This should work:

[[NSApplication sharedApplication] terminate: nil];

Reference

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜