引用于https://www.jianshu.com/p/d1715cad3d00
沙盒也叫沙箱,英文standbox,其原理是通过重定向技术,把程序生成和修改的文件定向到自身文件夹中。每个iOS 应用程序在安装时,都会创建属于自己的沙盒文件,应用程序不能直接访问其他应用程序的沙盒文件,当应用程序需要向外部请求或接收数据时,都需要经过权限认证,否则,无法获取到数据。即在沙盒机制下,每个程序之间的文件夹不能互相访问。iOS系统为了保证系统安全,采用了这种机制。
应用程序中所有的非代码文件都保存在沙盒中,比如图片、声音、属性列表,sqlite数据库和文本文件等。
如图所示:
沙盒根目录结构有4个文件夹,分别是Documents、Library、SystemData、tmp。
总结:用户生成的文件放在documents,自己项目的文件放在library/cache里面,如果你做个记事本的app,那么用户写了东西,总要把东西存起来。那么这个文件则是用户自行生成的,就放在documents文件夹里面。如果一个app,需要和服务器配合,经常从服务器下载东西,展示给用户看。那么这些下载下来的东西就放在library/cache。apple对这个很严格,放错了就会被拒。主要原因是ios的icloud的同步问题。
NSString *standboxHomeDirectory = NSHomeDirectory();
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *tmpDirectory = NSTemporaryDirectory();
NSSearchPathForDirectoriesInDomains 是用来查找文件路径的函数,其返回值是一个数组,这个数组中只有一个NSString类型元素,这个元素保存的就是查找的路径。
NSSearchPathForDirectoriesInDomains(<#NSSearchPathDirectory directory#>, <#NSSearchPathDomainMask domainMask#>, <#BOOL expandTilde#>)
其中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
};
+(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;
}
-(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");
}
}
-(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");
}
}
-(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);
}
- (BOOL)isSxistAtPath:(NSString *)filePath{
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isExist = [fileManager fileExistsAtPath:filePath];
return isExist;
}
- (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;
}
}
有两种方法可以获取: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);
NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:iOSPath];
// 移动到文件末尾
[fileHandle seekToEndOfFile];
[fileHandle writeData:[@"defg" dataUsingEncoding:NSUTF8StringEncoding]];
// 同步文件
[fileHandle synchronizeFile];
// 关闭
[fileHandle closeFile];
-(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(@"删除成功");
}
}
- (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");
}
}
文件的其他操作可参考如下链接:
https://blog.csdn.net/baozi520cc/article/details/18311849