objective-c - 由于 URL 协议(protocol),NSApplicationDelegate 应用程序处于事件状态

标签 objective-c cocoa macos

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

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

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

最佳答案

您需要使用 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 中注册您的方案。

关于objective-c - 由于 URL 协议(protocol),NSApplicationDelegate 应用程序处于事件状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3115657/

相关文章:

ios - ios中出现UISwipeGestureRecogniser时的UIImageView动画

ios - 试图了解 didselectrowatindexpath 发生了什么。应用程序崩溃,消息索引 1 超出范围 [0 .. 0]'

swift - NSSetUncaughtExceptionHandler 在 Swift 3 中没有触发?

ruby - 如何在 OS X 上不使用 RVM 来安装 Ruby?

macos - 在非事件 NSWindow 中刷新 NSTableView

objective-c - 弹出菜单定位项 :atLocation:inView: hangs when switching to another application

c++ - 前向声明的缺点?

ios - Objective-C 中的多个 NSURLConnection 委托(delegate)

macos - 使用 GetKeys 函数获取键盘状态

java - 如何指定在 NSMenuItem(最小化窗口指示器)中显示菱形?