-1

我正在尝试使用以下功能将带有 URL 的内容下载到我的 iPad (cachedDir ? ) 中的本地文件系统:

- (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 拒绝,我必须为我的路径选择哪个目录,以及如何检索我保存的数据?

有人可以帮忙吗?

4

2 回答 2

1

存储您想要保留的文档的最佳位置是您的应用程序的 Documents 直接。您可以像这样找到它的路径:

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

Apple 有一份关于 iOS 文件系统和存储特定类型文件的有用文档:http: //developer.apple.com/library/mac/#documentation/FileManagement/Conceptual/FileSystemProgrammingGUIide/FileSystemOverview/FileSystemOverview.html

于 2012-08-24T08:37:47.700 回答
0

试试这个我的代码,使用下面的代码在文档中保存 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);
于 2012-08-24T08:47:06.783 回答