来自UIWebVIew的带有外部应用程序的iOS Open Document [英] iOS Open Document with External App from UIWebVIew

查看:87
本文介绍了来自UIWebVIew的带有外部应用程序的iOS Open Document的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIWebView 。当单击包含文档的链接时,我想在外部应用程序(办公室,openoffice等)中打开该文档,但不要在 UIWebView 中打开。

I have a UIWebView. When a link which contains a document is clicked, I want to open that document in a external app (office, openoffice, etc) but not inside the UIWebView.

我知道如何为电子邮件做这件事:

I know how to do it for emails:

- (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;

}

有什么方法可以这样做但是对于文件?
谢谢!

Is there any way to do this but for documents? Thanks!

编辑:
这就是我所做的:

That's what I have done:

- (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 仅显示邮件邮件应用。我需要用于打开 pdf docx 等文件的应用程序。

The problem now is that the UIDocumentInteractionController only shows Mail and Messages apps. I need the apps for opening pdf, docx, etc files.

有什么想法吗?谢谢!

推荐答案

[UIWebViewDelegate webView:shouldStartLoadWithRequest:navigationType:navigationType:] 您可以检查URL是否看起来像是指向文档。最简单的方法就是查找pdf,doc等。如果你想要它更好,那么使用UTTypes。

In [UIWebViewDelegate webView: shouldStartLoadWithRequest: navigationType: navigationType:] you can check if the URL looks like it points to a document. The simplest way to do that is just to look for pdf, doc etc. If you want it to be nicer, then use UTTypes.

NSURL* possibleDocumentURL = [request mainDocumentURL];
NSString* extension = [[possibleDocumentURL lastPathComponent] pathExtension];
// TODO: check ending

如果它是文档的URL,那么只需下载该文件。您现在可以打开 DocumentPreviewController UIDocumentInteractionController QLPreviewController 。用户仍然需要选择文件的位置。

If it's a URL to a document, then just download the document. You can now either open a DocumentPreviewController, UIDocumentInteractionController or QLPreviewController. The user will still need to select where the file should go.

如果要打开它的应用程序支持自定义URL方案,您可以传递URL或文件在没有用户交互的情况下直接使用它。

If the app you want to open it in supports a custom URL scheme you could pass the URL or the file itself directly to it without user interaction.

这篇关于来自UIWebVIew的带有外部应用程序的iOS Open Document的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆