沙盒之iOS笔记摘录

目录

床前明月光,疑是地上霜。 举头望明月,低头思故乡。

前言

1. 沙盒

    当iOS系统安装应用时,会为该应用分配一块独立的空间(用来存放该应用的系统文件和缓存),该应用只能访问该空间中的资源,这种机制称之为沙盒机制。

 有4个文件夹:
    1、应用名.app
    应用程序包:程序本身,包含资源。是经过加签的,运行时不能修改。
    2、Documents
    存储用户下载或保存的数据。会被iTunes备份。
    3、Library      
    Caches文件夹:存储SDWebImg...等缓存。不会被iTunes备份。
    Preferences文件夹:会被iTunes备份。存储系统偏好设置NSUserdefaults。
    4、temp         
    存储临时文件。不会被iTunes备份,应用销毁后即清除。
NSHomeDirectory() 沙盒根目录
    // 存储路径
    NSString *path=[NSString stringWithFormat:@"%@/Documents/1.src",NSHomeDirectory()];

    // 获取Documents目录路径
    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) firstObject];
    // 获取Library的目录路径
    NSString *libDir = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES) lastObject];
    // 获取cache目录路径
    NSString *cachesDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES) firstObject];
    // 获取tmp目录路径
    NSString *tmpDir =NSTemporaryDirectory();
/*

第一个参数:想要查找的目录
ypedef 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 NS_ENUM_AVAILABLE(10_8, NA) = 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
};

第二个参数:从哪个路径区域查找
typedef NS_OPTIONS(NSUInteger, NSSearchPathDomainMask) {
   NSUserDomainMask =1,      // 用户的主目录
   NSLocalDomainMask =2,     // 当前机器的本地目录
   NSNetworkDomainMask =4,    //在网络中公开可用的位置
   NSSystemDomainMask =8,    // 被苹果系统提供的,不可更改的位置 (/System)
   NSAllDomainsMask = 0x0ffff  // 上述所有及未来的位置
};

第三个参数:true时显示具体路径,false时使用~代替主目录路径。
*/

  // [NSBundle mainBundle] 返回的路径是  .../项目名.app/
  NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"apple"ofType:@"png"];
  UIImage *appleImage = [[UIImage alloc] initWithContentsOfFile:imagePath];

2. 下载手机应用的沙盒目录

当项目遇到保存录音内容等需求时,会需要查看应用沙盒目录。

步骤

1. XCode的Window下点击Devices and Simulators
2. 单击installapp中的指定应用,点击设置按钮,点击DownloadContainer
3. 找到下载的.xcappdata文件,右键查看包内容
XCode的Window下点击Devices and Simulators
单击installapp中的指定应用,点击设置按钮,点击DownloadContainer
找到下载的xcap文件,右键查看包内容
Documents
Library
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 版本记录 前言 我们有时候需要在沙盒中存储东西,所以总有需求就是查看沙盒文件里面存储的文件,这就需要我们查看沙盒文...
    刀客传奇阅读 4,812评论 0 7
  • 要说一天,先从星期五说起。 本周末,三大任务:完成约稿,完成讲座提纲,完成优质课教案 周末在读书,思考, 睡觉,煮...
    躲进小楼看灯火阅读 183评论 0 0
  • 二姐上周领了结婚证,周五我回家的时候,好奇地跟我妈打听详情。 因为二姐夫工作在北京,而北京房子又太贵,所以他们选了...
    小萘阅读 309评论 0 3
  • 上高中时,我多买了几把蜡烛, 想推销给楼上楼下的同学, 由于比商店便宜, 大家纷纷来买 我借势多批发, 很多同学主...
    九叔绝技研究所阅读 1,259评论 29 58
  • 条码是由一组规则排列的条、空以及对应的字符组成的标记,“条”指对光线反射率较低的部分,“空”指对光线反射率较高的部...
    bole泉子阅读 388评论 0 0