Download / retrieve pdf with asihttprequest

断了今生、忘了曾经 提交于 2019-12-25 18:18:31

问题


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);


来源:https://stackoverflow.com/questions/12105782/download-retrieve-pdf-with-asihttprequest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!