次のコードを使用して、一時書き込みファイルを作成するためだけにデータベースファイルを他のフォルダーにコピーしています。
BOOL success;
NSArray*dirPath;
NSString*docDir;
NSString*databasePath;
NSString*databaseName=@"EXPENSES";
//path for database
dirPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docDir=[dirPath objectAtIndex:0];
databasePath=[docDir stringByAppendingPathComponent:databaseName];
NSLog(@" docDir %@",docDir);
//check if present
NSFileManager*fm=[NSFileManager defaultManager];
success=[fm fileExistsAtPath:databasePath];
if(success)
{
NSLog(@"DATA BASE Already present");
}
else
{
//Copy from bundle to DocumentsDirectory on first run. Where DB won't be available in DocumentsDirectory.
NSString*bundlePath=[[NSBundle mainBundle] pathForResource:@"EXPENSES" ofType:@""];
NSError*error;
success=[fm copyItemAtPath:bundlePath toPath:databasePath error:&error];
if(success)
{
NSLog(@"DATA BASE Created successfully");
}
} // End of else when DB not present in documents directory.
しかし、ファイルがコピーされていない代わりに、アプリがエラーでクラッシュしています。