views:

88

answers:

3

What method must I implement in my cocoa application’s delegate so that on launch, it’ll open a url? (http/https, in this case) I’ve already implemented the url schemes, I just need to know how I can get my application to open on a url notification.

Update: I’m sorry, I wasn’t very clear. My application IS a browser that support https/http urls, but can only open them when it’s already running. What can I do to implement support for open urls in my app on launch?

A: 

When an application finishes launching on OS X, NSApp (the global NSApplication instance for the program) sends its delegate the applicationDidFinishLaunching: message (via the notification system). You can implement that method in your delegate to handle the notification and open a browser window in response, using NSWorkspace. Something like the following would work:

// Your NSApp delegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://www.example.com/"]];
}
mipadi
I know this bit already, but thanks! What I meant is what method can I implement that will allow my application to open urls itself? My app can already handle opening urls when its running, but is only launched when a url is clicked elsewhere.
oneinfiniteloop
A: 

It's not a delegate method. You need to implement an Apple Event handler for the getURL event.

As luck would have it, this is exactly the case Apple uses to demonstrate implementing an Apple Event handler.

Peter Hosey
A: 

I already had implemented the getURL event, so that alone isn’t enough to get the application to open a url on launch. The trick is that the AppleEvent must be installed in applicationWillFinishLaunching: not applicationDidFinishLaunching:. Otherwise, the event isn’t sent at all because the app hasn’t registered it in time.

oneinfiniteloop