ios 从 url 下载文件并保存到 iPhone 上的资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2102267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Downloading a file from url and saving to resources on iPhone
提问by msk
Is it possible to download a file (i.e. an sqlite database file) from the Internet into your iPhone application programmatically for later use within the application?
是否可以以编程方式从 Internet 下载文件(即 sqlite 数据库文件)到您的 iPhone 应用程序中以供以后在应用程序中使用?
I am trying using NSURLConnection
, but not able to save the file.
我正在尝试使用NSURLConnection
,但无法保存文件。
Here is the example code I am trying:
这是我正在尝试的示例代码:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *file = [NSString stringWithFormat:@"http://en.wikipedia.org/wiki/Text_file"];
NSURL *fileURL = [NSURL URLWithString:file];
NSURLRequest *req = [NSURLRequest requestWithURL:fileURL];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:req delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[self.fileData setLength:0];
self.totalFileSize = [NSNumber numberWithLongLong:[response expectedContentLength]];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.fileData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSArray *dirArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSLog(@"%@", [dirArray objectAtIndex:0]);
NSString *path = [NSString stringWithFormat:@"%@/blah.text", [dirArray objectAtIndex:0]];
if ([self.fileData writeToFile:path options:NSAtomicWrite error:nil] == NO) {
NSLog(@"writeToFile error");
}
else {
NSLog(@"Written!");
}
}
采纳答案by Jordan
What is fileData? How is it defined and initialized?
什么是文件数据?它是如何定义和初始化的?
fileData = [NSMutableData data];
should be somewhere after:
应该在某处之后:
NSURLConnection *conn = [NSURLConnection connectionWithRequest:req delegate:self];
You have to initialize fileData and retain it per memory management rules.
您必须初始化 fileData 并根据内存管理规则保留它。
Try it with my answer. It Works!
用我的答案试试。有用!
回答by slf
You need to expand the path with stringByAppendingPathComponent
您需要扩展路径 stringByAppendingPathComponent
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"bookdb.sql"];
回答by SURESH SANKE
Try this code in a method and execute,
在方法中尝试此代码并执行,
NSURL *url=[NSURL URLWithString:@"http://en.wikipedia.org/wiki"];
NSData *dbFile = [[NSData alloc] initWithContentsOfURL:url];
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent]stringByAppendingPathComponent:@"Documents"]];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"Text_file.txt"];
[dbFile writeToFile:filePath atomically:YES];
回答by Ans
This can be useful for you.
这对你很有用。
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 automically:YES];
}
Note that this is a writeToFile
method is synchronous, so it will halt/block your UI.
请注意,这是一个writeToFile
同步方法,因此它会暂停/阻止您的 UI。