gpt4 book ai didi

ios - 如何在 iOS 中使用 UIDocument 获取远程文档?

转载 作者:行者123 更新时间:2023-11-28 22:07:59 24 4
gpt4 key购买 nike

我想将 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com