Download / retrieve pdf with asihttprequest

2019-09-20 17:45发布

问题:

I'm trying to download content with an URL to a local file system in my iPad (cachedDir ? ) with this function:

- (IBAction)download:(id)sender {

NSURL *url = [NSURL URLWithString:@"http:www.google.de"];

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
[request setDownloadDestinationPath:@"/Users/ben/Desktop/my_file.pdf"]; // Which //directory??

}

Which directory do I have to choose for my path if I want to store the data as long as possible without getting rejected by Apple, and how do I retrieve the data I've saved?

Can somebody help?

回答1:

The best place to store documents that you want to keep around is your application's Documents directly. You can find the path to it like so:

// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

Apple have a useful piece of documentation about the iOS file system and where to store particular types of files: http://developer.apple.com/library/mac/#documentation/FileManagement/Conceptual/FileSystemProgrammingGUide/FileSystemOverview/FileSystemOverview.html



回答2:

Try out this my code for save pdf in document using below code

NSString *downloadUrl=[NSURL URLWithString:@"http:www.google.de"];
 NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:downloadUrl]];

  //Store the Data locally as PDF File

 NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]  resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];
  NSString *pdf1 = @"title.pdf";
  NSString *filePath = [resourceDocPath stringByAppendingPathComponent:pdf1];

 [pdfData writeToFile:filePath atomically:YES];

and retrive your file using

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSURL *pdfURL = [[NSBundle bundleWithPath:[paths objectAtIndex:0]] URLForResource:[NSString stringWithFormat:@"title.pdf"] withExtension:nil];
    NSLog(@"pDF URl %@", pdfURL);

    pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);