在建工程的时候,就把已经有资料的数据库给扔到工程里了,在模拟器中可以修改,但是用真机进行测试的时候,就不能进行修改了.
原因是苹果限制了,不能在它给的那三个文件夹之外修改其他文件。所以修改,判断如果Documents文件夹下没有数据库,就复制过来。
-(void)copyFileDatabase
{
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentLibraryFolderPath = [documentsDirectory stringByAppendingPathComponent:DATABASENAME];
if ([[NSFileManager defaultManager] fileExistsAtPath:documentLibraryFolderPath]) {
//NSLog(@"文件已经存在了");
}else {
NSString *resourceSampleImagesFolderPath =[[NSBundle mainBundle]
pathForResource:@"floorKing"
ofType:@"db"];
NSData *mainBundleFile = [NSData dataWithContentsOfFile:resourceSampleImagesFolderPath];
[[NSFileManager defaultManager] createFileAtPath:documentLibraryFolderPath
contents:mainBundleFile
attributes:nil];
}
}