如何在Objective-C应用程序中退出?

pro*_*eek 10 objective-c protocol-handler

我有一个启动一些应用程序的URLHandler,主要代码如下.

@implementation URLHandlerCommand

- (id)performDefaultImplementation {
    NSString *urlString = [self directParameter];

    NSLog(@"url :=: %@", urlString);

    NSTask *task;
    task = [[NSTask alloc] init];
    [task setLaunchPath: @"/usr/bin/open"];

    NSArray *arguments;
    arguments = [NSArray arrayWithObjects: @"-a", @"Path Finder.app", urlString, nil];
    [task setArguments: arguments];

    NSPipe *pipe;
    pipe = [NSPipe pipe];
    [task setStandardOutput: pipe];

    NSFileHandle *file;
    file = [pipe fileHandleForReading];

    [task launch];

    return nil;
}
Run Code Online (Sandbox Code Playgroud)

由于此例程的目标是启动另一个应用程序,我想在启动App后使此URLHandler退出.我怎样才能做到这一点?

Yuj*_*uji 19

  1. 您不必open使用NSTask... open只需调用Launch Services,其中一些功能可直接从NSWorkspace获得.

  2. 要退出,你只需打电话[[NSApplication sharedApplication] terminate:nil].见NSApplication文档.

  • 我建议不要退出`exit(0)`.可可可能想在戒烟前做一些清理工作.我不确定Cocoa是否用`atexit`注册这些清理函数,所以我建议你调用` - [NSApplication terminate:]`. (3认同)
  • 谢谢你的回答,似乎`exit(0)`正常工作. (2认同)