11

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?

dbr
  • 165,801
  • 69
  • 278
  • 343
prosseek
  • 182,215
  • 215
  • 566
  • 871

2 Answers2

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

Yuji
  • 34,103
  • 3
  • 70
  • 88
  • 2
    Thanks for the answer, it seems that `exit(0)` just works fine. – prosseek Feb 19 '11 at 04:28
  • 3
    I recommend against `exit(0)`. Cocoa might want to do some cleaning up before quitting. I'm not sure if Cocoa registers these cleaning up functions with `atexit`, so I suggest you to call `-[NSApplication terminate:]`. – Yuji Feb 19 '11 at 04:32
1

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.

Community
  • 1
  • 1
aiham
  • 3,614
  • 28
  • 32