我有一个 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
只显示Mail
和Messages
应用程序。我需要打开pdf
,docx
等文件的应用程序。有什么想法吗?谢谢!