开发者

NSApplicationDelegate application active because of URL Protocol

开发者 https://www.devze.com 2023-01-04 21:30 出处:网络
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.Th

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

精彩评论

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

关注公众号