» 首頁 » 討論區 » iOS程式討論 »將檔案從APP複制到Document下

將檔案從APP複制到Document下

發表人: Seachaos
積分: 2432
發表時間: 2012-01-30 01:14:42
因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]