Can anyone please provide or show me how to download a PDF asynchronously if a local file doesnt exist.
My code is as follows:
NSURL *url = [NSURL URLWithString:@"http://www.url.com"];
NSString *tempDownloadPath = [[self documentsDirectory]
stringByAppendingString:@"test.pdf"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadDestinationPath:[self documentsDirectory]];
[request setTemporaryFileDownloadPath:tempDownloadPath];
[request setDelegate:self];
[request startAsynchronous];
Once it is complete I try and call this
[aWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[self documentsDirectory] pathForResource:@"test" ofType:@"pdf"]isDirectory:NO]]];
however it either crashes or doesn't load anything inside my web view.
Any suggestions?
EDIT WITH SELF DOCUMENTSDIRECTORY
You need to put your file in some place accessible to the UIWebView and then point it there. You've not included how you're creating [self documentsDirectory]
and you're just appending a string rather than using the path append for your temporary location. You're also not telling ASIHTTPRequest what actual file name to use for the final document, just the directory to put it in, so it's likely not even being saved. Additionally, the UIWebView load request is incorrect.
Here's how to create your path for telling ASIHTTPRequest where to put the file.
EDITED to change temporary file location to the NSCachesDirectory instead, so that it will be automatically cleared out if the download fails with partial data
// SAVED PDF PATH
// Get the Document directory
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// Add your filename to the directory to create your saved pdf location
NSString *pdfLocation = [documentDirectory stringByAppendingPathComponent:@"test.pdf"];
// TEMPORARY PDF PATH
// Get the Caches directory
NSString *cachesDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
// Add your filename to the directory to create your temp pdf location
NSString *tempPdfLocation = [cachesDirectory stringByAppendingPathComponent:@"test.pdf"];
// Tell ASIHTTPRequest where to save things:
[request setTemporaryFileDownloadPath:tempPdfLocation];
[request setDownloadDestinationPath:pdfLocation];
Then when your delegate receives notification of the file download being complete, tell the UIWebView where to find the file, again using the proper methods.
// If you've stored documentDirectory or pdfLocation somewhere you won't need one or both of these lines
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *pdfLocation = [documentDirectory stringByAppendingPathComponent:@"test.pdf"];
// Now tell your UIWebView to load that file
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:pdfLocation]]];
I think the error is that you're downloading the file to the documents directory and then you're looking for the file in the main bundle. You should look for it in the documents directory.