我有一个 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
等文件的应用程序。
有什么想法吗?谢谢!
最佳答案
关于iOS 使用来自 UIWebVIew 的外部应用程序打开文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28279463/