如何下载文件并保存在本地iPhone应用程序中?

发布于 2024-12-09 20:08:43 字数 182 浏览 0 评论 0原文

我在本地保存了一个 xml 文件,并使用 NSXmlParser(存储在 Resource 文件夹中)显示详细信息。但是,现在我想从 url(从客户端)下载新的 xml,并且需要将该新的 xml 文件存储在本地,并且还需要从应用程序中删除现有的文件。我该怎么做?有什么建议吗?有示例代码/项目可供参考吗?请帮我。感谢您阅读我蹩脚的英语。感谢是提前的。

I have locally saved one xml file and showing the details by using NSXmlParser(Which is stored in Resource folder). But, now i want to download new xml from url(from client side) and need to store that new xml file in local and also need to delete existing one from the application. How can i do this? Any suggestions? There is any sample code/project for reference? Please help me. Thanks for reading my poor english. Thanks is advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巴黎夜雨 2024-12-16 20:08:43

虽然您可以从资源目录中删除文件并保存新文件,但我的方法是主要在应用程序目录(如文档目录或缓存目录)上执行文件操作。

首先,您将把 xml 文件从应用程序资源保存到缓存目录,然后从那里访问文件。您还可以从资源目录中删除文件,不再需要它。
互联网上任何可用的新文件都首先从缓存中删除旧文件,然后将新文件添加到缓存目录中。

整个事情如下:-

    //get your cachedirectory path
NSArray *imagepaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachdirectory = [[NSString alloc] initWithString:[imagepaths objectAtIndex:0]];

//first save your file from resource directory to app's cache  directory
NSString * path = [[NSBundle mainBundle] pathForResource:@"fileinresource" ofType:@"xml"];
NSData *filedata = [NSData dataWithContentsOfFile:path];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *savename = [cachdirectory stringByAppendingPathComponent:@"mynewfile.xml"];
[fileManager createFileAtPath:savename contents:filedata attributes:nil];       

//remove file from resource directory
[fileManager removeItemAtPath:path error:nil];

//new file from internet is avilable then do following:first remove old file from cache
[fileManager removeItemAtPath:cachdirectory error:nil];

//save new file from internet
NSData *newfiledata=[NSData dataWithContentsOfURL:[NSURL URLWithString:myxmlurlstringpath]];
[fileManager createFileAtPath:savename contents:newfiledata attributes:nil];        

although u can delete file from yr resource directory and save new one but the my way is to perform file operation mostly on app diretories like document directory or cache directory.

first u will to save yr xml file from app resource to cache directory then access file from there.And u can also remove file from resource directory ,its no longer needed.
any new file available in internet then first remove old one from cache and add new one to cache directory.

whole thing is as follows:-

    //get your cachedirectory path
NSArray *imagepaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachdirectory = [[NSString alloc] initWithString:[imagepaths objectAtIndex:0]];

//first save your file from resource directory to app's cache  directory
NSString * path = [[NSBundle mainBundle] pathForResource:@"fileinresource" ofType:@"xml"];
NSData *filedata = [NSData dataWithContentsOfFile:path];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *savename = [cachdirectory stringByAppendingPathComponent:@"mynewfile.xml"];
[fileManager createFileAtPath:savename contents:filedata attributes:nil];       

//remove file from resource directory
[fileManager removeItemAtPath:path error:nil];

//new file from internet is avilable then do following:first remove old file from cache
[fileManager removeItemAtPath:cachdirectory error:nil];

//save new file from internet
NSData *newfiledata=[NSData dataWithContentsOfURL:[NSURL URLWithString:myxmlurlstringpath]];
[fileManager createFileAtPath:savename contents:newfiledata attributes:nil];        
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文