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

时间:2011-02-19 04:08:29

标签: 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;
}

由于此例程的目标是启动另一个应用程序,我想在启动应用程序后退出此URLHandler。我怎么能这样做?

2 个答案:

答案 0 :(得分:19)

  1. 您不必使用open ... NSTask启动open只需调用Launch Services,其中一些功能可直接从NSWorkspace获取{3}}。

  2. 要退出,只需拨打[[NSApplication sharedApplication] terminate:nil]即可。请参阅NSApplication documentation

答案 1 :(得分:1)

这已经在这里得到了解答:Proper way to exit iPhone application?但是你也应该看一下August的回答。虽然它不是公认的答案,但由于苹果公司建议不要强行退出iPhone应用程序,因此它已经被推高,并且也遵循苹果公司的既定标准。