目前,我检测UIWebView是否通过检查当前URL来加载pdf文件。接下来,我使用ASIHTTPRequest库下载pdf文件。问题是,如果文件显示在UIWebView中,它是否已经在某处下载,所以我下载了这个文件两次。如何在我的UIWebView中加载此文件?
目的是将我的UIWebView中加载的文件存储在我的Document目录中。
答案 0 :(得分:1)
以下是如何在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];
答案 1 :(得分:0)
我可以建议的是要么第二次下载它,要么把它放在临时存储中,然后把它放在UIWebView上,当用户询问时,然后将它放在你想要的临时存储中。