我想将pdf文件显示为预览,并在我的应用程序中显示PDF文件,但是我要使用的pdf是从URL获取并在不使用Web视图的情况下在我的文档中显示那些pdf文件,但我只想在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: invalid scheme http. Only the file scheme is supported
要解决此问题,您需要下载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];
}
以及以下委托方法:
- (UIViewController*)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
return self;
}
由于AFNetworking
上面示例中使用的大多数类仍然是标准的,因此不使用该代码将大体上相似。我碰巧在这种情况下使用AFNetworking来简化一些设置,并自动使用它提供的类别UIProgress
来为我更新进度条。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句