Skip to content

Instantly share code, notes, and snippets.

@sahara-ooga
Last active November 4, 2023 08:37
Show Gist options
  • Save sahara-ooga/1cf8b7a425a396129087a4486bfa1819 to your computer and use it in GitHub Desktop.
Save sahara-ooga/1cf8b7a425a396129087a4486bfa1819 to your computer and use it in GitHub Desktop.
iOSでのファイルの保管場所をObjective-Cで取得する

iOSでのデータの保存場所は、

  • /アプリ名.app
  • /Documents
  • /Library/Caches
  • /Library/Preferences
  • /tmp

がある。

/Library/PreferencesNSUserDefaultsが使うディレクトリで、専用のファイルパスの取得方法はない。 それ以外のファイルパスの取得方法は次のようなものが用意されている。

    //アプリ名.app
    NSLog(@"%@", [[NSBundle mainBundle] bundlePath]);
    // /var/containers/Bundle/Application/B3170E91-BEF9-4B10-AE52-97BBA2E2B8B9/FindDirectory.app

    //Documents 第二引数と第三引数は固定
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSLog(@"%@",paths[0]);
    // /var/mobile/Containers/Data/Application/F4562F8F-427E-431C-A67B-47B76F2DC437/Documents

    
    // /Libray/Caches 第二引数と第三引数は固定
    NSArray *cashesDirectoryPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES);
    NSLog(@"%@",cashesDirectoryPaths[0]);
    // /var/mobile/Containers/Data/Application/6813E7AF-8B0C-45B0-8355-A2F0ECC7F985/Library/Caches
    
    // /tmp
    NSLog(@"%@", NSTemporaryDirectory());
    // /private/var/mobile/Containers/Data/Application/6813E7AF-8B0C-45B0-8355-A2F0ECC7F985/tmp/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment