使用asihttprequest下载/检索pdf

我试图通过这个函数在我的iPad(cachedDir?)下载带有URL的内容到本地文件系统:

- (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?? } 

如果我想尽可能长时间地存储数据而不被Apple拒绝,那么我需要为我的pathselect哪个目录?如何检索我已经保存的数据?

有人可以帮忙吗?

存储您想要保留的文档的最佳位置是您的应用程序的文档直接。 你可以像这样find它的path:

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

苹果有一个关于iOS文件系统的文档,以及在哪里存储特定types的文件: http : //developer.apple.com/library/mac/#documentation/FileManagement/Conceptual/FileSystemProgrammingGUide/FileSystemOverview/FileSystemOverview.html

试试这个我的代码保存PDF文档使用下面的代码

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

并回顾你的文件使用

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