【问题标题】:NSApplicationDelegate application active because of URL Protocol由于 URL 协议,NSApplicationDelegate 应用程序处于活动状态
【发布时间】:2011-03-08 03:17:39
【问题描述】:

嘿!我将我的 Mac 应用程序设置为在 myApp:// 上启动,在浏览器(如 Safari)中调用协议,但是当该协议调用应用程序时,我似乎无法执行操作。委托方法必须是这样的:

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

我不知道这个,因为我是Mac开发的新手,但我对iPhone开发有点擅长,所以我知道iPhone的开发方式,但不知道Mac的开发方式

【问题讨论】:

  • NSApplicationDelegate 的文档是怎么说的?
  • 我检查了文档,这次我实际上一无所获。我遍历了每个 NSApplicationDelegate 和 NSApplication 函数,寻找任何看起来可以工作但没有找到任何东西的东西。我知道这是可能的,因为 iTunes 和 Mail 做到了。

标签: objective-c cocoa macos


【解决方案1】:

您需要使用NSAppleEventManager。你知道,AppKit 早于 Internet,OS X 仍然主要处理文件而不是 URL 方案等。UIKit 有时更好。阅读此Apple doc

在实践中:首先在applicationWillFinishLaunching:注册一个handler

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

然后实现处理程序

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

您还需要在Info.plist注册您的方案。

【讨论】:

    猜你喜欢
    • 2011-06-11
    • 1970-01-01
    • 2019-07-04
    • 2013-05-08
    • 2016-02-04
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多