views:

21

answers:

1

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

A: 

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