美文网首页
iOS沙盒机制及文件相关操作

iOS沙盒机制及文件相关操作

作者: _宁静致远_ | 来源:发表于2017-10-11 00:28 被阅读0次

跑一个例子

 NSString *homePath = NSHomeDirectory();
 NSArray *docArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *docPath = docArray.firstObject;
 NSArray *libArray = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
 NSString *libPath = libArray.firstObject;
    
 NSString *tmpPath = NSTemporaryDirectory();
    
 NSArray *cacheArray = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
 NSString *cachePath = cacheArray.firstObject;
    
 NSLog(@"docPath = [%@]",docPath);
 NSLog(@"tmpPath = [%@]",tmpPath);
 NSLog(@"cachePath = [%@]",cachePath);
 NSLog(@"homePath = [%@]",homePath);
 NSLog(@"libPath = [%@]",libPath);

记住这个例子里面的API,用于获取HomeDir, Tmp,Directory,Doc。其中Directory下面还有Cache,preference两个子目录。
输出结果如下

docPath = [/Users/haojin/Library/Developer/CoreSimulator/Devices/340ABE0A-7DF3-4233-85EF-B7AEFDC6AA44/data/Containers/Data/Application/BC63D060-F02C-4790-BA58-788E4A3C7972/Documents]
tmpPath = [/Users/haojin/Library/Developer/CoreSimulator/Devices/340ABE0A-7DF3-4233-85EF-B7AEFDC6AA44/data/Containers/Data/Application/BC63D060-F02C-4790-BA58-788E4A3C7972/tmp/]
cachePath = [/Users/haojin/Library/Developer/CoreSimulator/Devices/340ABE0A-7DF3-4233-85EF-B7AEFDC6AA44/data/Containers/Data/Application/BC63D060-F02C-4790-BA58-788E4A3C7972/Library/Caches]
homePath = [/Users/haojin/Library/Developer/CoreSimulator/Devices/340ABE0A-7DF3-4233-85EF-B7AEFDC6AA44/data/Containers/Data/Application/BC63D060-F02C-4790-BA58-788E4A3C7972]
libPath = [/Users/haojin/Library/Developer/CoreSimulator/Devices/340ABE0A-7DF3-4233-85EF-B7AEFDC6AA44/data/Containers/Data/Application/BC63D060-F02C-4790-BA58-788E4A3C7972/Library]

结构如下

模拟器中app沙盒目录结构示意
注意函数NSSearchPathForDirectoriesInDomains(Directory,domain,expand)使用
  • expand : 设置YES是绝对路径,NO则输出~/xxx
  • 此函数在类NSPathUtilities中

沙盒中各个文件夹的作用

  • Document

苹果建议将程序中生成的或是浏览的数据放入这个文件夹,itunes备份时候会备份这个文件夹。

  • Library

存储程序的默认设置或者其他状态信息

  • Library/Cache

缓存数据,itunes不会备份,应用程序退出后此文件夹不会删除

  • tmp

临时文件,iphone重启后该文件会丢失

读写文件

写文件

写一个hello world的txt文件

- (void)writeFile
{
    NSArray *docArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docPath = docArray.firstObject;
    if (docPath.length > 0) {
        NSString *content = @"hello world";
        NSString *filePath = [docPath stringByAppendingString:@"/hello.txt"];
        [content writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    }
}

读文件

读取上述hello world文件

- (void)readFile
{
    NSArray *docArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docPath = docArray.firstObject;
    NSString *filePath = [docPath stringByAppendingString:@"/hello.txt"];
    if (filePath.length > 0) {
        NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"content = [%@]",content);
    }
}

检查文件是否存在

- (BOOL)checkIfFileExist
{
    NSArray *docArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docPath = docArray.firstObject;
    NSString *filePath = [docPath stringByAppendingString:@"/hello.txt"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    return  [fileManager fileExistsAtPath:filePath];
}

在指定path创建目录和文件

- (void)generateDirAndFileInDocument
{
    NSArray *docArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docPath = docArray.firstObject;
    NSString *newDirPath = [docPath stringByAppendingString:@"/subDir"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (docPath.length > 0) {
        [fileManager createDirectoryAtPath:newDirPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSString *filePath = [newDirPath stringByAppendingString:@"/file.txt"];
    [fileManager createFileAtPath:filePath contents:[@"hello world" dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
}

搜索目录

- (void)searchDocument
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *docArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docPath = docArray.firstObject;
    NSArray *array = [fileManager subpathsOfDirectoryAtPath:docPath error:nil];
    for (NSString *path in array) {
        NSLog(@"path = [%@]",path);
    }
}

bundle相关

mainBundle,放各类资源,例如图片,声音,视频,nib等。
分别打印mainBundle地址和sandbox地址如下

  • sandbox
[/Users/haojin/Library/Developer/CoreSimulator/Devices/340ABE0A-7DF3-4233-85EF-B7AEFDC6AA44/data/Containers/Data/Application/45738BE3-1F9C-48E7-B0D5-744458E27A0B/Library]
  • mainBundle
[/Users/haojin/Library/Developer/CoreSimulator/Devices/340ABE0A-7DF3-4233-85EF-B7AEFDC6AA44/data/Containers/Bundle/Application/81E61DD0-45FF-4C1F-AC5D-CF93F31F86EF/TestSandBox.app]

实验了一下,如果在工程中添加一个txt文件,那么编译后在mainBundle目录下会出现该目录。即.app目录下。作为应用程序的资源文件被复制过来。

相关文章

  • iOS沙盒机制及文件相关操作

    跑一个例子 记住这个例子里面的API,用于获取HomeDir, Tmp,Directory,Doc。其中Direc...

  • 使用沙盒的正确姿势

    在学习iOS存储方法之前,先了解一下iOS存储机制——沙盒应用沙盒机制:每个iOS应用都有自己的应用沙盒(文件系统...

  • 【IOS开发基础系列 整理】IOS沙盒机制专题

    整理自如下文章: iOS学习之iOS沙盒(sandbox)机制和文件操作(一) http://blog.csdn....

  • ios 沙盒、文件操作与app安装路径

    ios沙盒机制 沙盒包括3个文件夹及app程序打包的目录 Document:程序创建或应用浏览产生的文件数据,当我...

  • 沙盒

    一、iOS沙盒机制介绍(1)概念:每个ios应用都有自己的应用沙盒,应用沙盒就是文件系统目录,与其他应用放入文件 ...

  • iOS开发-隐私权限

    背景 iOS的安全机制——沙盒限制了应用程序执行各种操作的权限。沙盒实际就是程序的系统文件目录,非代码文件都在此保...

  • iOS文件目录介绍

    沙盒文件目录 在iOS系统的安全机制,每个APP都有自己的文件目录,且只能访问自己的文件目录。该机制被称为沙盒机制...

  • iOS 沙盒路径及文件操作

    前言 开发中经常用到获取文件路径以及文件的操作,今天就来讨论一下路径和文件相关的东西。 沙盒 沙盒是苹果的安全机制...

  • iOS 沙盒路径及文件操作

    前言 开发中经常用到获取文件路径以及文件的操作,今天就来讨论一下路径和文件相关的东西。 沙盒 沙盒是苹果的安全机制...

  • iOS数据持久化(1)-文件读写

    1.沙盒机制 iOS采用沙盒机制管理应用,应用只能访问自己目录下的文件。iOS不像Android,没有SD卡概念,...

网友评论

      本文标题:iOS沙盒机制及文件相关操作

      本文链接:https://www.haomeiwen.com/subject/trliyxtx.html