ios - 如何在ios中的缓存文件路径中保存多个xml文件?

标签 ios xml download

我将 xml 文件保存在缓存文件路径中,如下所示:

// Determile cache file path
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 filePathl = [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0],@"list.xml"];   

// Download and write to file
 NSURL *url = [NSURL URLWithString:detail_product_listing_rss];
 NSData *urlData = [NSData dataWithContentsOfURL:url];
 [urlData writeToFile:filePathl atomically:YES];

但是使用此代码我可以检索最近使用的 xml 文件的数据。有人可以告诉我如何保存多个 xml 文件以供离线使用吗?

最佳答案

您需要用不同的名称保存 xml。否则它会覆盖旧的 xml。

为此目的保留一个整数值,如果您希望在应用程序重新启动后执行此操作,请将其保留在 NSUserDefaults 中.

 int posValue = [[[NSUserDefaults standardUserDefaults] objectForKey:@"lastXml"] intValue];

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 filePathl = [NSString stringWithFormat:@"%@/list_%d.xml", [paths objectAtIndex:0],posValue];   

 NSURL *url = [NSURL URLWithString:detail_product_listing_rss];
 NSData *urlData = [NSData dataWithContentsOfURL:url];
 [urlData writeToFile:filePathl atomically:YES];

 [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:posValue+1] forKey:@"lastXml"];

关于ios - 如何在ios中的缓存文件路径中保存多个xml文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13977064/

相关文章:

Android:直接从浏览器下载文件

iphone - 字幕和静态 UILabel 文本

iphone - 从iOS Phonegap中的URL下载图像

java - SAXParserFactory URL 超时

html - XSL - 对于每个不工作

xml - xml 值中的尖括号

java - 将文件从服务器传输到客户端会导致下载速度非常低

ios - Xcode 6 GM - 无法使用 Playground

ios - 没有 Storyboard 的 Swift UITableViewCell 不使用自动布局

android - ENOENT(没有这样的文件或目录)尝试打开使用 AsyncTask 从 URL 下载的文件