由于URL协议,NSApplicationDelegate应用程序处于活动状态

Han*_*kke 3 macos cocoa objective-c

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

- (void)applicationDidBecomeActiveByURL:(NSURL *)protocol;
Run Code Online (Sandbox Code Playgroud)

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

Yuj*_*uji 8

你需要使用NSAppleEventManager.你知道,AppKit早于Internet,OS X仍然主要用于不在URL方案上的文件等.UIKit有时候更好.阅读此Apple文档.

在实践中:首先,在中注册一个处理程序 applicationWillFinishLaunching:

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

然后实现处理程序

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

您还需要注册您的计划Info.plist.