將檔案從APP複制到Document下
發表人:
Seachaos
積分: 2432
積分: 2432
因iOS的APP包內的檔案是不可讀寫的
所以像是SQLite之類的檔案要讀寫的話就要copy到Document下才可以
以下是copy database.sqlite檔案的範例
[sea:javaCode]
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *documentPath = [self applicationDocumentsDirectory];
NSURL *databasePath = [documentPath URLByAppendingPathComponent:@"database.sqlite"];
if(![fileManager fileExistsAtPath:[databasePath path]]){
NSLog(@"File not exists, will copy at:%@",[databasePath absoluteString]);
NSError *error;
NSURL *resourceDBPath = [[NSBundle mainBundle] URLForResource:@"database" withExtension:@"sqlite"];
if(![fileManager copyItemAtURL:resourceDBPath toURL:databasePath error:&error]){
NSLog(@"Copy success.");
}else{
NSLog(@"Copy database failed! %@",error);
}
}else{
NSLog(@"Database exists.");
}
[/sea]
所以像是SQLite之類的檔案要讀寫的話就要copy到Document下才可以
以下是copy database.sqlite檔案的範例
[sea:javaCode]
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *documentPath = [self applicationDocumentsDirectory];
NSURL *databasePath = [documentPath URLByAppendingPathComponent:@"database.sqlite"];
if(![fileManager fileExistsAtPath:[databasePath path]]){
NSLog(@"File not exists, will copy at:%@",[databasePath absoluteString]);
NSError *error;
NSURL *resourceDBPath = [[NSBundle mainBundle] URLForResource:@"database" withExtension:@"sqlite"];
if(![fileManager copyItemAtURL:resourceDBPath toURL:databasePath error:&error]){
NSLog(@"Copy success.");
}else{
NSLog(@"Copy database failed! %@",error);
}
}else{
NSLog(@"Database exists.");
}
[/sea]