从url下载文件并保存到iPhone上的资源
问题描述:
是否可以通过编程方式从Internet上将文件(即sqlite数据库文件)下载到您的iPhone应用程序*以后使用?我想用NSURLConnection
,但不能保存文件。从url下载文件并保存到iPhone上的资源
这里是示例代码我想:
- (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!");
}
}
答
什么是FILEDATA?它是如何定义和初始化的?
fileData = [NSMutableData data];
后应某处:
NSURLConnection *conn = [NSURLConnection connectionWithRequest:req delegate:self];
你必须初始化FILEDATA并妥善保存每个内存管理规则。
尝试用我的答案。有用!
答
您需要扩展的路径与stringByAppendingPathComponent
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"bookdb.sql"];
答
的方法试试这个代码并执行,
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];
+0
URL到文件目录比较好用这种方式 - > NSURL * documentsDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; 你可以这样[URL路径]来获取路径 – coolcool1994 2016-10-26 23:25:26
答
这可能是对你有用。
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];
}
注意,这是一个writeToFile
方法是同步的,所以它会停止/阻止你的用户界面。
究竟是什么在这里失败?如果([self.fileData writeToFile:path options:NSAtomicWrite error:nil == NO]){ – Vladimir 2010-01-20 15:08:58
NSLog(@“writeToFile error”); } 我得到“writeToFile错误” – msk 2010-01-20 15:12:01