嘿!我将我的 Mac 应用程序设置为在浏览器(如 Safari)中调用 myApp://协议(protocol)启动,但当应用程序被该协议(protocol)调用时,我似乎无法执行操作。委托(delegate)方法必须类似于:
- (void)applicationDidBecomeActiveByURL:(NSURL *)protocol;
我不知道,因为我是Mac开发的新手,但我对iPhone开发有点擅长,所以我知道iPhone的开发方式,但不知道Mac的开发方式
最佳答案
您需要使用 NSAppleEventManager
。你知道,AppKit 早于 Internet,OS X 仍然主要处理文件而不是 URL 方案等。UIKit 有时更好。阅读此 Apple doc .
实践中:首先,在applicationWillFinishLaunching:
-(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/