从UIWebView打开iOS外部应用程序的文档

3

我有一个 UIWebView。当点击包含文档的链接时,我希望在外部应用程序(office、openoffice等)中打开该文档,而不是在 UIWebView 中。

我知道如何为电子邮件执行此操作:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ([[[request URL] scheme] isEqual:@"mailto"]) {
    [[UIApplication sharedApplication] openURL:[request URL]];
    return NO;
}
return YES;

有没有办法对文档进行类似操作呢?

谢谢!

编辑: 这是我所做的:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSURL* possibleDocumentURL = [request mainDocumentURL];
    NSString* extension = [[possibleDocumentURL lastPathComponent] pathExtension];

    if ([[[request URL] scheme] isEqual:@"mailto"]) {
        [[UIApplication sharedApplication] openURL:[request URL]];
        return NO;

    }else if ([extension isEqualToString:@"pdf"] || [extension isEqualToString:@"docx"]){

        NSURLRequest *req = [[NSURLRequest alloc] initWithURL:possibleDocumentURL];
        [NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse
*resp, NSData *respData, NSError *error){ stringByAppendingString:names[names.count-1]];
        NSString *fileName = @"myFile";
        NSString * path = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName];
        NSError *errorC = nil;
        BOOL success = [respData writeToFile:path
                                         options:NSDataWritingFileProtectionComplete
                                           error:&errorC];

        if (success) {
            self.documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:path]];
             self.documentController.delegate = self;
             [self.documentController presentOptionsMenuFromRect:CGRectZero inView:self.view animated:YES];
         } else {
             NSLog(@"fail: %@", errorC.description);
         }
    }];

        return NO;
   }

   return YES; 
}

现在的问题是UIDocumentInteractionController只显示MailMessages应用程序。我需要打开pdfdocx等文件的应用程序。
有什么想法吗?谢谢!
2个回答

4

2
[UIWebViewDelegate webView: shouldStartLoadWithRequest: navigationType: navigationType:]中,您可以检查URL是否指向文档。最简单的方法是查找pdf、doc等文件扩展名。如果您想让它更好,可以使用UTTypes。
NSURL* possibleDocumentURL = [request mainDocumentURL];
NSString* extension = [[possibleDocumentURL lastPathComponent] pathExtension];
// TODO: check ending

如果是文档的URL,那么只需下载文档。现在你可以打开一个DocumentPreviewControllerUIDocumentInteractionControllerQLPreviewController。用户仍然需要选择文件要放在哪里。
如果你想要打开的应用程序支持自定义URL方案,你可以直接将URL或文件本身传递给它,无需用户交互。

谢谢你的回复 :) 是否可能直接打开外部应用程序?例如,如果我收到一个 .docx 文件,我想打开 office,如果我收到一个 pdf 文件,我想打开 adobe reader,以此类推... 我已经尝试了 DocumentPreviewController 但是无法让它工作。 - Ale
只有当外部应用程序支持自定义URL方案时,您才能直接打开它们。请参见http://stackoverflow.com/questions/8988563/use-adobe-reader-to-open-a-pdf-from-own-app-using-webviewcontroller Adobe Reader没有自定义URL。Good Reader有http://www.goodiware.com/gr-man-manage.html#link - orkoden

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接