How to quit itself in Objective-C application?

六月ゝ 毕业季﹏ 提交于 2019-12-01 14:36:34

问题


I have a URLHandler that launches some application, the main code is as follows.

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

As the goal of this routine is to launch another application, I'd like to make this URLHandler quit after launching an App. How can I do that?


回答1:


  1. You don't have to launch open using NSTask... open just calls into Launch Services, some of whose functionality is directly available from NSWorkspace.

  2. To quit, you just call [[NSApplication sharedApplication] terminate:nil]. See NSApplication documentation.




回答2:


This has been answered here: Proper way to exit iPhone application? however you should also take a look at August's answer. Although it's not the accepted answer, it has been voted higher and it also follows Apple's set standards since Apple advises against force quitting iPhone applications.



来源:https://stackoverflow.com/questions/5048777/how-to-quit-itself-in-objective-c-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!