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!