作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想将 pdf 文件显示为预览并在我的应用程序中显示 PDF 文件,但我想使用的 pdf 是从 URL 获取并在我的文档中显示这些 pdf 文件而不使用 Web View ,但我想在 UIDocumentation 中显示仅有的。但如果我从 URL 使用,应用程序会崩溃?如何解决这个问题,?
NSURL *URL = [NSURL URLWithString:@"http://golearning.blinkweb.com/uploads.00612104/00388749.pdf"];
if(URL)
{
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL];
self.documentInteractionController.delegate = self;
[self.documentInteractionController presentPreviewAnimated:YES];
}
最佳答案
答案就在 Chaithra 的评论中:
UIDocumentInteractionController:无效的方案 http。仅支持文件方案
要解决此问题,您需要下载 PDF 文件,为其获取本地 URL,然后将其传递给 UIDocumentInteractionController
。使用 AFNetworking 作为基线,以下对我有用:
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSProgress *progress = nil;
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:&progress destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL *documentsDirectory = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory
inDomain:NSUserDomainMask
appropriateForURL:nil
create:NO error:nil];
return [documentsDirectory URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
[self displayDocument:filePath];
}];
[downloadTask resume];
我的 displayDocument:
方法如下所示:
- (void)displayDocument:(NSURL*)document {
UIDocumentInteractionController *documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:document];
documentInteractionController.delegate = self;
[documentInteractionController presentPreviewAnimated:YES];
}
以及以下委托(delegate)方法:
- (UIViewController*)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
return self;
}
如果不使用 AFNetworking
,代码将大致相似,因为上面示例中使用的大多数类都是标准的。在这种情况下,我碰巧使用 AFNetworking 来简化一些设置,并自动使用它围绕 UIProgress
提供的类别来为我更新进度条。
关于ios - 如何在 iOS 中使用 UIDocument 获取远程文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23509655/
我是一名优秀的程序员,十分优秀!