如何下载PDF并将其存储在iPhone本地?
问题描述:
我能够从网站成功查看PDF。我希望能够将该PDF下载到设备,然后在本地访问该文件。
I am able to successfully view a PDF from a website. I want to be able to download that PDF to the device, then access that file locally.
当应用程序打开时,它将检查在线PDF的日期。如果它比本地存储的PDF更新,应用程序将下载新的PDF,否则它将打开本地存储的PDF。
When the app is opened, it will check the online PDF's date. If it is newer than the locally-stored PDF, the app will download the new one, otherwise it opens the locally-stored PDF.
我目前使用的代码:
PDFAddress = [NSURL URLWithString:@"http://www.msy.com.au/Parts/PARTS.pdf"];
request = [NSURLRequest requestWithURL:PDFAddress];
[webView loadRequest:request];
webView.scalesPageToFit = YES;
我怎么能实现这个目标?
How am I able to achieve this?
答
我找到了一个自己试过的方法:
I have found one method which I tried myself:
// Get the PDF Data from the url in a NSData Object
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[
NSURL URLWithString:@"http://www.example.com/info.pdf"]];
// Store the Data locally as PDF File
NSString *resourceDocPath = [[NSString alloc] initWithString:[
[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent]
stringByAppendingPathComponent:@"Documents"
]];
NSString *filePath = [resourceDocPath
stringByAppendingPathComponent:@"myPDF.pdf"];
[pdfData writeToFile:filePath atomically:YES];
// Now create Request for the file that was saved in your documents folder
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView setUserInteractionEnabled:YES];
[webView setDelegate:self];
[webView loadRequest:requestObj];
这将在本地存储您的PDF并将其加载到您的UIWebView中。
This will store your PDF locally and load it into your UIWebView.
希望这可以解决您的问题。
Hope this solves your problem.