iOS Open Document with External App from UIWebVIew

2019-04-11 00:23发布

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!

EDIT: 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; 
}

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

Any ideas? Thanks!

2条回答
太酷不给撩
2楼-- · 2019-04-11 01:02

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

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.

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.

查看更多
何必那么认真
3楼-- · 2019-04-11 01:18

UIDocumentInteractionController will work for you.

Apple docs

Great step by step tutorial

查看更多
登录 后发表回答