从Cocoa中的URL Scheme捕获URL

时间:2012-07-25 09:18:09

标签: objective-c cocoa

我已经为我的应用添加了一个网址方案,当点击链接“my_scheme:// item_to_add”时,它会正确打开(或者如果已经打开则会正常显示)。

我找到了如何在iOS上捕获链接:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url;

任何人都可以在Mac上指出类似的方法,我可以捕获链接吗?

1 个答案:

答案 0 :(得分:1)

好的,再次找到答案;)

//Register to the AppleEventManager
NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
[appleEventManager setEventHandler:self   andSelector:@selector(handleGetURLEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];

并实现选择器方法:

- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:       (NSAppleEventDescriptor *)replyEvent
{
    NSString* url = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
    NSLog(@"%@", url);
}