2014-06-27 115 views
0

我在Mac可可应用程序中实现了自定义URL方案。它的工作原理,但我有几个问题。在可可应用程序中使用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]; 

回答

0

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]; 
} 
相关问题