沙盒之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

你可能感兴趣的:(沙盒之iOS笔记摘录)