IOS开发之沙盒机制和文件操作

引用于https://www.jianshu.com/p/d1715cad3d00
沙盒也叫沙箱,英文standbox,其原理是通过重定向技术,把程序生成和修改的文件定向到自身文件夹中。每个iOS 应用程序在安装时,都会创建属于自己的沙盒文件,应用程序不能直接访问其他应用程序的沙盒文件,当应用程序需要向外部请求或接收数据时,都需要经过权限认证,否则,无法获取到数据。即在沙盒机制下,每个程序之间的文件夹不能互相访问。iOS系统为了保证系统安全,采用了这种机制。

应用程序中所有的非代码文件都保存在沙盒中,比如图片、声音、属性列表,sqlite数据库和文本文件等。

沙盒的目录结构

如图所示:
在这里插入图片描述
沙盒根目录结构有4个文件夹,分别是Documents、Library、SystemData、tmp。

  • Documents:用于应用程序在运行时生成的一些相对重要、需要长久保存的文件数据,用户数据或其它应该定期备份的信息(比如:游戏进度存档、应用程序个人设置等等),通过 iTunes、iCloud 备份时, 会备份这个目录下的数据。我们可以将应用程序的数据文件保存在该目录下。不过这些数据类型仅限于不可再生的数据,可再生的数据文件应该存放在Library/Cache目录下。
  • Library:包含Caches和Preferences⼦目录。开发者最常用的文件夹,可以自定义子文件夹,存储程序的默认设置或其它状态信息。
    • Library/Caches 主要是缓存文件,目录用于存放应用程序专用的支持文件,保存应用程序再次启动过程中需要的信息,如存放缓存文件,从网络上下载的文件或者数据(如:音乐缓存、图片缓存等),此目录下的文件不会在应用退出时自动删除 ,需要程序员手动清除改目录下的数据。iTunes、iCloud 备份时不会备份此目录下的数据。主要用于保存应用在运行时生成的需要长期使用的数据,一般用于存储体积较大,不需要备份的非重要数据。
    • Library/Preferences 保存应⽤用的所有偏好设置。存放的是基于NSUserDefaults的设置数据,文件格式为 “plist”。应用的一些功能会在该目录中查找相应设置的信息,iTunes、iCloud备份时会备份此目录下的数据。该目录由系统自动管理,通常用来储存一些基本的应用配置信息。比如账号密码、自动登录等。
  • tmp :保存应⽤运⾏时所需的临时数据,使用完毕后再将相应的文件从该⽬目录删除。应⽤没有运⾏时,该目录下的东西随时有可能被系统清理掉,例如:系统磁盘存储空间不足的时候。iPhone在重启时,会丢弃所有的tmp文件。

总结:用户生成的文件放在documents,自己项目的文件放在library/cache里面,如果你做个记事本的app,那么用户写了东西,总要把东西存起来。那么这个文件则是用户自行生成的,就放在documents文件夹里面。如果一个app,需要和服务器配合,经常从服务器下载东西,展示给用户看。那么这些下载下来的东西就放在library/cache。apple对这个很严格,放错了就会被拒。主要原因是ios的icloud的同步问题。

沙盒路径

1.获取程序的根目录

NSString *standboxHomeDirectory = NSHomeDirectory();

2.获取Documents目录

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

3.获取Library目录

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

4.获取Caches目录

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

5.获取tmp目录

NSString *tmpDirectory = NSTemporaryDirectory();

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

};

文件管理之NSFileManager

1.创建文件夹

+(BOOL)creatDir:(NSString *)path{
    if (path.length==0) {
        return NO;
    }
    // 初始化文件管理类 (单例)
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isSuccess = YES;
    // 判断文件夹是否存在
    BOOL isExist = [fileManager fileExistsAtPath:path];
    if (isExist==NO) {
        NSError *error;
        if (![fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error]) {
            isSuccess = NO;
            NSLog(@"creat Directory Failed:%@",[error localizedDescription]);
        }
    }
    return isSuccess;
}

2.创建文件

-(void)createFile{
	NSString *pathString = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    pathString = [pathString stringByAppendingPathComponent:@"liu"];
    NSString *iOSPath = [pathString stringByAppendingPathComponent:@"iOS.txt"];
    BOOL isSuccess = [fileManager createFileAtPath:iOSPath contents:nil attributes:nil];
    if (isSuccess) {
        NSLog(@"success");
    } else {
        NSLog(@"fail");
    }
}

3.写入文件

-(void)writeFile{
    NSString *pathString = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    pathString = [pathString stringByAppendingPathComponent:@"liu"];
    NSString *iOSPath = [pathString stringByAppendingPathComponent:@"iOS.txt"];
    NSString *content = @"我要写数据啦";
    BOOL isSuccess = [content writeToFile:iOSPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    if (isSuccess) {
        NSLog(@"write success");
    } else {
        NSLog(@"write fail");
    }
}

4.读文件

-(void)readFileContent{
    NSString *pathString = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    pathString = [pathString stringByAppendingPathComponent:@"liu"];
    NSString *iOSPath = [pathString stringByAppendingPathComponent:@"iOS.txt"];
    NSString *content = [NSString stringWithContentsOfFile:iOSPath encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"read success: %@",content);
}

5.判断文件是否存在

- (BOOL)isSxistAtPath:(NSString *)filePath{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isExist = [fileManager fileExistsAtPath:filePath];
    return isExist;
}

6.计算文件大小

- (unsigned long long)fileSizeAtPath:(NSString *)filePath{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isExist = [fileManager fileExistsAtPath:filePath];
    if (isExist){
        unsigned long long fileSize = [[fileManager attributesOfItemAtPath:filePath error:nil] fileSize];
        return fileSize;
    } else {
        NSLog(@"file is not exist");
        return 0;
    }
}

7.获取指定文件夹内所有文件

有两种方法可以获取:subpathsOfDirectoryAtPath 和subpathsAtPath

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSFileManager *fileManage = [NSFileManager defaultManager];

NSString *myDirectory = [documentsDirectory stringByAppendingPathComponent:@"liu"];

NSArray *file = [fileManage subpathsOfDirectoryAtPath: myDirectory error:nil];

NSArray *files = [fileManage subpathsAtPath: myDirectory ];

NSLog(@"%@",files);

8.追加写入

NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:iOSPath];
// 移动到文件末尾
[fileHandle seekToEndOfFile];
[fileHandle writeData:[@"defg" dataUsingEncoding:NSUTF8StringEncoding]];
// 同步文件
[fileHandle synchronizeFile];
// 关闭
[fileHandle closeFile];

8.删除文件

-(void)deleteFile{
    NSString *pathString = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    pathString = [pathString stringByAppendingPathComponent:@"liu"];
    
    [fileManager createDirectoryAtPath:pathString withIntermediateDirectories:YES attributes:nil error:nil];
    
    NSString *iOSPath = [pathString stringByAppendingPathComponent:@"iOS.txt"];
    BOOL isSuccess = [fileManager createFileAtPath:iOSPath contents:nil attributes:nil];
    if (isSuccess) {
        NSLog(@"success");
    } else {
        NSLog(@"fail");
    }
    if ([fileManager removeItemAtPath:iOSPath error:nil]) {
        NSLog(@"删除成功");
    }
}

9.移动文件

- (void)moveFileName
{
    NSString *pathString = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    pathString = [pathString stringByAppendingPathComponent:@"liu"];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *filePath = [pathString stringByAppendingPathComponent:@"iOS.txt"];
    NSString *moveToPath = [pathString stringByAppendingPathComponent:@"iOS.txt"];
    BOOL isSuccess = [fileManager moveItemAtPath:filePath toPath:moveToPath error:nil];
    if (isSuccess) {
        NSLog(@"rename success");
    }else{
        NSLog(@"rename fail");
    }
}

NSFileManager 和 NSFileHandle 常用操作

文件的其他操作可参考如下链接:
https://blog.csdn.net/baozi520cc/article/details/18311849

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值