NSFileManagerを使用して、ファイルがライブラリフォルダに既に存在するかどうかを確認できます。存在しない場合は移動します。移動が成功した場合は、古いファイルを削除して新しいパスを返し、そうでない場合は古いパスを返します。下記をご覧ください:
...
NSString *mySQLfilePath = [self getFilePathWithName:@"database.sqlite"];
...
- (NSString *)getFilePathWithName:(NSString *)filename
{
NSString *libPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *fileLibPath = [libPath stringByAppendingPathComponent:filename];
NSString *fileDocPath = [docPath stringByAppendingPathComponent:filename];
if ([[NSFileManager defaultManager] fileExistsAtPath:fileLibPath]) {
// file already exists in Library folder
return fileLibPath;
} else if ([[NSFileManager defaultManager] fileExistsAtPath:fileDocPath]) {
// file exists in Documents folder, so move it to Library
NSError *error = nil;
BOOL moved = [[NSFileManager defaultManager] moveItemAtPath:fileDocPath toPath:fileLibPath error:&error];
NSLog(@"move error: %@", error);
// if file moved, you can delete old Doc file if you want
if (moved) [self deleteFileAtPath:fileDocPath];
// if file moved successfully, return the Library file path, else, return the Documents file path
return moved ? fileLibPath : fileDocPath;
}
// file doesn't exist
return nil;
}
- (void)deleteFileAtPath:(NSString *)filePath
{
if ([[NSFileManager defaultManager] isDeletableFileAtPath:filePath]) {
NSError *error = nil;
[[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
NSLog(@"delete error: %@", error);
} else {
NSLog(@"can not delete file: %@", filePath);
}
}
ご不明な点がございましたら、お気軽にお問い合わせください。そして、ちょうど訪問者のための参照:
https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html
あなたは、単にファイルをライブラリディレクトリにし、その場合であれば、テストそこから、それを使用し、それがドキュメントディレクトリにあります場合は、それを移動することはできませんそれを使い始めるのですか?私は合併症を理解していない。 – Droppy