3

Hey! I have my Mac application set up to launch on myApp:// protocol is called in a browser, like Safari, but I cannot seem to be able to do an action when the application is called by that protocol. The delegate method would have to be something like:

- (void)applicationDidBecomeActiveByURL:(NSURL *)protocol;

I don't know this because I am new at Mac developing, but I am somewhat good at iPhone developing, so I know the iPhone development way, but not the Mac development way

Yuji
  • 34,103
  • 3
  • 70
  • 88
Hank Brekke
  • 2,024
  • 2
  • 24
  • 33
  • What does the documentation for NSApplicationDelegate say? – Gary Jun 25 '10 at 05:12
  • 1
    I checked the documentation and I actually found absolutely nothing this time. I went through every NSApplicationDelegate and NSApplication function looking for anything that looks like it'd work and didn't find anything. I know it's possible, because iTunes and Mail do it. – Hank Brekke Jun 25 '10 at 05:33
  • and this was a dupilcate of http://stackoverflow.com/questions/1991072/how-to-handle-with-a-default-url-scheme – Yuji Jun 25 '10 at 06:17

1 Answers1

10

You need to use NSAppleEventManager. You know, AppKit predates Internet, OS X still works mainly on files not on URL schemes, etc. UIKit is sometimes better. Read this Apple doc.

In practice: First, register a handler in applicationWillFinishLaunching:

-(void)applicationWillFinishLaunching:(NSNotification *)aNotification {
    NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
    [appleEventManager setEventHandler:self 
                           andSelector:@selector(handleGetURLEvent:withReplyEvent:)
                         forEventClass:kInternetEventClass andEventID:kAEGetURL];
}

and then implement the handler

- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent
{
    NSString *urlAsString = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
    ... do something ... 
}

You also need to register your scheme in Info.plist.

Yuji
  • 34,103
  • 3
  • 70
  • 88