【问题标题】:Create a new document with url scheme in a cocoa app在可可应用程序中使用 url 方案创建一个新文档
【发布时间】:2014-06-27 00:37:34
【问题描述】:

我在 Mac 可可应用中实现了自定义 URL 方案。它有效,但我遇到了几个问题。

1) 我应该将此代码放在基于文档的应用程序的什么位置来处理 URL 事件?我在我的-windowControllerDidLoadNib: 中有它,但如果由于尚未设置文档而关闭应用程序,它就不起作用。

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

2) 如何将解析后的数据从 URL 发送到新文档?我正在使用我的 -handleGetURLEvent:withReplyEvent: 方法创建新文档,就像这样。

[[NSDocumentController sharedDocumentController] newDocument:nil];

【问题讨论】:

    标签: objective-c macos cocoa url-scheme


    【解决方案1】:

    1) 我应该将此代码放在基于文档的应用程序的什么位置来处理 URL 事件?

    很可能在您的应用程序中委托 applicationDidFinishLaunching 方法。

    2) 如何将解析后的数据从 URL 发送到新文档?

    - (void)handleGetURLEvent:(NSAppleEventDescriptor *)event 
           withReplyEvent:(NSAppleEventDescriptor *)replyEvent {
        NSString *urlString = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
        NSLog(@"URL: %@", urlString);
        NSString *httpString = [urlString substringFromIndex:7];
        NSLog(@“HTTP URL: %@", httpString);
        [self openNewDocumentAtURL:[NSURL URLWithString:httpString]];
    }
    
    - (void)openNewDocumentAtURL:(NSURL *)absoluteURL {
        NSDocumentController *docController = [NSDocumentController sharedDocumentController];
        [docController newDocument:nil];
        [(ResponseDocument *)[docController currentDocument] updateURL:absoluteURL];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-19
      • 2019-12-26
      • 2015-12-18
      • 2017-08-02
      • 2016-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多