栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

沙盒中的文件目录结构及用途

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

沙盒中的文件目录结构及用途

沙盒路径
  • Documents
  • Library
      • Cache
      • Preferences:
      • Application Support:
      • Frameworks:
  • tmp


1、获取程序相关的目录:

//1、 获取沙盒主目录路径
NSString *homeDir = NSHomeDirectory();
// 2、获取Documents目录路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
// 3、获取Caches目录路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDir = [paths objectAtIndex:0];
// 4、获取tmp目录路径
NSString *tmpDir = NSTemporaryDirectory();
// 5、获取当前程序包中一个图片资源(apple.png)路径
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"apple" ofType:@"png"];
UIImage *appleImage = [[UIImage alloc] initWithContentsOfFile:imagePath];

另有NSSearchPathForDirectoriesInDomains 是用来查找文件路径的函数,其返回值是一个数组,这个数组中只有一个NSString类型元素,这个元素保存的就是查找的路径。

NSSearchPathForDirectoriesInDomains(<#NSSearchPathDirectory directory#>, <#NSSearchPathDomainMask domainMask#>, <#BOOL expandTilde#>)
  • NSSearchPathDirectory: 表示需要查找的是某个文件夹
  • NSSearchPathDomainMask:NSUserDomainMask 表示在用户的主目录中查找
  • expandTilde:YES 表示返回路径展开
    其中NSSearchPathDirectory是个枚举:
typedef NS_ENUM(NSUInteger, NSSearchPathDirectory) {
    NSApplicationDirectory = 1,             // supported applications (Applications)
    NSDemoApplicationDirectory,             // unsupported applications, demonstration versions (Demos)
    NSDeveloperApplicationDirectory,        // developer applications (Developer/Applications). DEPRECATED - there is no one single Developer directory.
    NSAdminApplicationDirectory,            // system and network administration applications (Administration)
    NSLibraryDirectory,                     // various documentation, support, and configuration files, resources (Library)
    NSDeveloperDirectory,                   // developer resources (Developer) DEPRECATED - there is no one single Developer directory.
    NSUserDirectory,                        // user home directories (Users)
    NSDocumentationDirectory,               // documentation (Documentation)
    NSDocumentDirectory,                    // documents (Documents)
    NSCoreServiceDirectory,                 // location of CoreServices directory (System/Library/CoreServices)
    NSAutosavedInformationDirectory API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)) = 11,   // location of autosaved documents (Documents/Autosaved)
    NSDesktopDirectory = 12,                // location of user's desktop
    NSCachesDirectory = 13,                 // location of discardable cache files (Library/Caches)
    NSApplicationSupportDirectory = 14,     // location of application support files (plug-ins, etc) (Library/Application Support)
    NSDownloadsDirectory API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0)) = 15,              // location of the user's "Downloads" directory
    NSInputMethodsDirectory API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)) = 16,           // input methods (Library/Input Methods)
    NSMoviesDirectory API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)) = 17,                 // location of user's Movies directory (~/Movies)
    NSMusicDirectory API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)) = 18,                  // location of user's Music directory (~/Music)
    NSPicturesDirectory API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)) = 19,               // location of user's Pictures directory (~/Pictures)
    NSPrinterDescriptionDirectory API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)) = 20,     // location of system's PPDs directory (Library/Printers/PPDs)
    NSSharedPublicDirectory API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)) = 21,           // location of user's Public sharing directory (~/Public)
    NSPreferencePanesDirectory API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)) = 22,        // location of the PreferencePanes directory for use with System Preferences (Library/PreferencePanes)
    NSApplicationScriptsDirectory API_AVAILABLE(macos(10.8)) API_UNAVAILABLE(ios, watchos, tvos) = 23,      // location of the user scripts folder for the calling application (~/Library/Application Scripts/code-signing-id)
    NSItemReplacementDirectory API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)) = 99,	    // For use with NSFileManager's URLForDirectory:inDomain:appropriateForURL:create:error:
    NSAllApplicationsDirectory = 100,       // all directories where applications can occur
    NSAllLibrariesDirectory = 101,          // all directories where resources can occur
    NSTrashDirectory API_AVAILABLE(macos(10.8), ios(11.0)) API_UNAVAILABLE(watchos, tvos) = 102             // location of Trash directory

};

Documents

保存用户创建的文档文件的目录,用户可以通过文件分享分享该目录下的文件。保存应用运行时生成的需要持久化的数据,如游戏进度、涂鸦软件的绘图等信息,在iTunes和iCloud备份时会备份该目录。建议保存你希望用户看得见的文件。
2、获取Document目录

NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
Library

苹果不建议在该目录下保存任何用户相关数据,而是保存APP运行需要的修改数据,当然用户可以根据自己的实际需要进行保存。包含四个文件夹。
3、获取Library目录:

NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
Cache

建议保存数据缓存使用。在用户的磁盘空间已经使用完毕时有可能删除该目录下的文件,在APP使用期间不会删除,APP没有运行时系统有可能进行删除。需要持久化的数据建议不要保存在该目录下,以免系统强制删除。
保存应用运行时生成的需要持久化的数据,iTunes同步设备时不会备份该目录。一般存储体积大、不需要备份的非重要数据,如缓存图片或者离线数据(地图等)。
4、获取Caches目录:

NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
Preferences:

用户偏好存储目录,在使用NSUserDefaults或者CFPreferences接口保存的数据保存在该目录下,编程人员不需要对该目录进行管理。在iTunes和iCloud备份时会备份该目录。

Application Support:

建议用来存储除用户数据相关以外的所有文件,如游戏的新关卡。在iTunes和iCloud备份时会备份该目录。

Frameworks:

用来保存动态库的文件夹,在iOS系统中已不能使用,该目录可以忽略。

tmp

苹果建议该目录用来保存临时使用的数据,编程人员应该在数据长时间内不使用时主动删除该目录下的文件,在APP没有运行期间,系统可能删除该目录下的文件。在iTunes和iCloud备份时不会备份该目录。
保存应用运行时所需的临时数据,使用完毕后再将相应的文件从该目录删除。应用没有运行时,系统也可能会清除该目录下的文件。iTunes同步设备时不会备份该目录。重新手机、系统磁盘不足时都会被清理。

5、获取tmp目录:

NSString *tmpDirectory = NSTemporaryDirectory();
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/939041.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号