0

現在のバージョンのアプリケーションでは、NSDocumentDirectoryに2つの重要なアプリケーションファイル(Core Data.sqliteファイルとSettingsFile.plist)が保存されています。次のバージョンでは、これらのファイルをNSLibraryDirectoryに移動します。どちらも編集のためにユーザーが直接アクセスする必要はなく、私の研究では、これがこれらのファイルのそれぞれに最適なオプションであると思われます。新しいアプリケーションバージョンのiOSアプリケーションデータファイルをNSDocumentDirectoryからNSLibraryDirectoryに移動するにはどうすればよいですか?

現在のすべてのアプリケーションユーザーのファイルをNSDocumentDirectoryからNSLibraryDirectoryに移動する方法に関係します。私は、ユーザーのデータを失わないように非常に注意したいと思います。私は、これらのファイルを非常に安全な方法で移動することについて、どこに、どのように動き始めているのか分かりません。私は誰かが正しい方向に私を向けることを望んでいる。

+0

あなたは、単にファイルをライブラリディレクトリにし、その場合であれば、テストそこから、それを使用し、それがドキュメントディレクトリにあります場合は、それを移動することはできませんそれを使い始めるのですか?私は合併症を理解していない。 – Droppy

答えて

2

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

+0

'[NSHomeDirectory()stringByAppendingPathComponent:@"ライブラリ "];'?私はそうは思わない... – Droppy

+0

NSHomeDirectory(): '/アプリケーション/ DAF1FB3A-3ED3-464C-AAEB-CC21F3761355' NSLibraryDirectory:/アプリケーション/ DAF1FB3A-3ED3-464C-AAEB-CC21F3761355 /ライブラリ' NSDocumentsDirectory: '/ Application/DAF1FB3A-3ED3-464C-AAEB-CC21F3761355/Documents' – emotality

+0

あなたは' NSSearchPathForDirectoriesInDomains'を使うことになっています。 – Droppy

関連する問題