如何使用客观C在iOS上下载并保存文件? [英] How do I download and save a file locally on iOS using objective C?
本文介绍了如何使用客观C在iOS上下载并保存文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Objective-C的新手,我想从网络上下载一个文件(如果在网络服务器上已更改),并将其保存在本地,以便我的应用程序可以使用它。
I'm new to Objective-C and I want to download a file from the web (if it was changed on the webserver) and save it locally so it can be used by my application.
主要是要实现什么 wget --timestamp< url>
。
推荐答案
我不知道wget是什么,但是要从网上获取文件并将其存储在本地,可以使用NSData:
I'm not sure what wget is, but to get a file from the web and store it locally, you can use NSData:
NSString *stringURL = @"http://www.somewhere.com/thefile.png";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"filename.png"];
[urlData writeToFile:filePath atomically:YES];
}
这篇关于如何使用客观C在iOS上下载并保存文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文