开发者

NSApplicationDelegate application active because of URL Protocol

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 dev开发者_如何学编程eloping, so I know the iPhone development way, but not the Mac development way


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜