我必须下载数据库并替换沙箱中的现有数据库: 这是可行的方法:
DBHelpers *help=[[DBHelpers alloc] init];
NSString *targetPath=[[help DocumentsDirectory] stringByAppendingPathComponent:DATABASE_NAME];
NSLog(@"Target path: %@", targetPath);
NSFileManager *fileManager=[NSFileManager defaultManager];
//if([fileManager fileExistsAtPath:targetPath])
//{
// NSLog(@"Exists");
// return;
}
NSString *sourcePath=[help PathForResource:DATABASE_NAME];
NSLog(@"SourcePath path: %@", sourcePath);
NSError *error;
NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"www.hello.com/mydb.sqlite"]];
[data writeToFile:targetPath atomically:NO];
// [fileManager copyItemAtPath:sourcePath toPath:targetPath error:&error];
NSLog(@"Error %@", error);
答案 0 :(得分:5)
请考虑以下步骤:
NSData *fetchedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.myserver.com/files/DBName.sqlite"]]];
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDire ctory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"DBName.sqlite"];
[fetchedData writeToFile:filePath atomically:YES];
来自iphonedevsdk forum主题。