iOS 使用来自 UIWebVIew 的外部应用程序打开文档

标签 ios uiwebview external document

我有一个 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 等文件的应用程序。

有什么想法吗?谢谢!

最佳答案

UIDocumentInteractionController 将为您工作。

Apple docs

Great step by step tutorial

关于iOS 使用来自 UIWebVIew 的外部应用程序打开文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28279463/

相关文章:

ios - 自定义 UIView 的边框绘制不流畅

iOS 如何在应用程序的第二个屏幕中添加标签栏?

iphone - iCarousel 以编程方式停在索引处

ios - ADFS 登录页面自定义

java - 如何在 android studio 中编辑依赖项/外部库?

actionscript-3 - AS3 从外部 SWF 实例化类

ios - Ionic Framework 和 Firebase 3.x 版本 : This domain is not authorized for OAuth operations for your Firebase project

ios - UIWebView:停止滚动,但不要禁用它

javascript - 苹果手机SDK : How to get rid of the action sheet in UIWebView?

在 Windows 7 上使用外部程序时,git diff tmp 文件在 Windows 上无效