Question

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?

Was it helpful?

Solution 2

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.

OTHER TIPS

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/"]];
}

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.

To implement a protocol handler that you can select (in Safari preferences, for example) as the "default browser" and which will launch in response to HTTP / HTTPS, you need to do a few things.

  1. Add .scriptSuite and .scriptTerminology files to your project resources. These will tell Mac OS X that you'll be handling the GetURL command.

  2. Add a CFBundleURLTypes key to your Info.plist file listing the "URL Schemes" that your app will handle.

  3. Also in Info.plist, add the NSAppleScriptEnabled key with the value YES.

  4. Add a new class to your application as a subclass of NSScriptCommand and implement the -(id)performDefaultImplementation selector. From within this function you will find the clicked URL in [self directParameter]. Pass this on to your app's URL handler!

For the full details check out the article: http://www.xmldatabases.org/WK/blog/1154_Handling_URL_schemes_in_Cocoa.item

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top