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

2017-10-11  本文已影响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)使用

沙盒中各个文件夹的作用

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

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

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

临时文件,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地址如下

[/Users/haojin/Library/Developer/CoreSimulator/Devices/340ABE0A-7DF3-4233-85EF-B7AEFDC6AA44/data/Containers/Data/Application/45738BE3-1F9C-48E7-B0D5-744458E27A0B/Library]
[/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目录下。作为应用程序的资源文件被复制过来。

上一篇下一篇

猜你喜欢

热点阅读