gpt4 book ai didi

objective-c - 如何将加载到 UIWebView 中的 pdf 文件存储在我的文档目录中?

转载 作者:行者123 更新时间:2023-11-29 13:40:23 25 4
gpt4 key购买 nike

目前,我通过检查当前 URL 来检测 UIWebView 是否加载了 pdf 文件。接下来我下载带有 ASIHTTPRequest 库的 pdf 文件。问题是,如果文件显示在 UIWebView 中,是因为它已经下载到某处,所以我下载了这个文件两次。我怎样才能在我的 UIWebView 中加载这个文件?

目的是将加载到我的 UIWebView 中的这个文件存储在我的 Document 目录中。

最佳答案

以下是您可以在 iphone 应用程序中下载、阅读和本地存储 pdf 的方法,这样您就不必经常下载:

首先创建UIWebView并包含<<UIWebViewDelegate>>>

  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"YourPDF.pdf"];
if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]){ // if file not present
// download file , here "https://s3.amazonaws.com/hgjgj.pdf" = pdf downloading link
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"https://s3.amazonaws.com/hgjgj.pdf"]];
//Store the downloaded file in documents directory as a NSData format
[pdfData writeToFile:filePath atomically:YES];
}

NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[yourWebView setUserInteractionEnabled:YES];
[yourWebView setDelegate:self];
yourWebView.scalesPageToFit = YES;
[yourWebView loadRequest:requestObj];

或者,如果您只是想从资源文件夹中读取/加载 pdf,那么只需执行以下操作:

     NSString*  filePath= [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"pdf"]];
/// or you can even read docs file as : pathForResource:@"sample" ofType:@"docx"]
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[yourWebView setUserInteractionEnabled:YES];
[yourWebView setDelegate:self];
yourWebView.scalesPageToFit = YES;
[yourWebView loadRequest:requestObj];

关于objective-c - 如何将加载到 UIWebView 中的 pdf 文件存储在我的文档目录中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9357293/

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