4

アプリケーションにリソースとしてバンドルしたい.jsonファイルがあります。にサポートファイルを配置する必要があると書かれているファイルシステムプログラミングガイドを読みました<Application_Home>/Library/Application Support。では、アプリケーションをビルドする前に、.jsonファイルをこのディレクトリに配置するにはどうすればよいですか?

そして、実行時にファイルをどのように参照しますか?

4

2 に答える 2

5

ファイルが常に読み取り専用 (アプリの更新の一部としてのみ更新される) である場合は、プロジェクト内のアプリのリソースにファイルを追加します。次に、アプリのバンドルからファイルを読み取るだけです。

// assume a filename of file.txt. Update as needed
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"txt"];

アプリで初期ファイルを提供する必要があり、実行時に更新を行う必要がある場合は、上記のようにファイルをパッケージ化する必要がありますが、アプリを初めて実行するときは、アプリのサンドボックス内の書き込み可能な別の場所にファイルをコピーする必要があります。

// Get the Application Support directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *appSupportDirectory = [paths objectAtIndex:0];

// Create this path in the app sandbox (it doesn't exist by default)
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createDirectoryAtPath:appSupportDirectory withIntermediateDirectories:YES attributes:nil error:nil];

// Copy the file from the app bundle to the application support directory
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"txt"];
NSString *newPath = [appSupportDirectory stringByAppendingPathComponent:[filePath lastPathComponent]];
[fileManager copyItemAtPath:filePath toPath:newPath error:nil];
于 2012-11-19T23:37:14.243 に答える
0

ここに良い説明があります http://www.cocoawithlove.com/2010/05/finding-or-creating-application-support.html

ディレクトリは実行時に作成する必要があり、ファイルはメインバンドルからそこに移動され、リンクで説明されている方法を使用してアクセスできます。

于 2012-11-19T21:09:08.517 に答える