iOS的沙盒機制,應用只能訪問當前應用目錄下的文件內(nèi)容抽兆。應用內(nèi)所產(chǎn)生的如圖像识补、文件、緩存等內(nèi)容也都必須存儲在自己的沙盒內(nèi)辫红。
默認情況下凭涂,每個沙盒含有3個文件夾:Documents, Library 和 tmp。
其中Library包含Caches贴妻、Preferences目錄切油。
(完整路徑為:/Users/xxxx/Library/Developer/CoreSimulator/Devices)
沙盒結(jié)構(gòu)
Application:存放程序源文件,上架前經(jīng)過數(shù)字簽名名惩,上架后不可修改
Documents:常用目錄澎胡,iCloud備份目錄,存放數(shù)據(jù),這里不能存緩存文件,否則上架
不被通過
Library:
&Caches:存放體積大又不需要備份的數(shù)據(jù),比如下載的音樂,視頻,SDWebImage緩存等
&Preference:設置目錄娩鹉,iCloud會備份設置信息
tmp:存放臨時文件滤馍,不會被備份,而且這個文件下的數(shù)據(jù)有可能隨時被清除的可能
1,如何獲取沙盒根路徑
-(void)directoryHome{
NSString *directoryHome=NSHomeDirectory();
NSLog(@"app_home: %@",directoryHome);
}
2,獲取Documents目錄
-(NSString *)dirDoc{
//[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"app_home_doc: %@",documentsDirectory);
return documentsDirectory;
}
3,獲取Library目錄路徑:
//獲取Library目錄
-(void)dirLib{
//[NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryDirectory = [paths objectAtIndex:0];
NSLog(@"app_home_lib: %@",libraryDirectory);
}```
4,獲取Cache目錄路徑:
//獲取Cache目錄
-(void)dirCache{
NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [cacPath objectAtIndex:0];
NSLog(@"app_home_lib_cache: %@",cachePath);
}```
5,獲取Tmp目錄路徑:
//獲取Tmp目錄
-(void)dirTmp{
//[NSHomeDirectory() stringByAppendingPathComponent:@"tmp"];
NSString *tmpDirectory = NSTemporaryDirectory();
NSLog(@"app_home_tmp: %@",tmpDirectory);
}```
6,創(chuàng)建文件夾:
//創(chuàng)建文件夾
-(void *)createDir{
NSString *documentsPath =[self dirDoc];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
}
創(chuàng)建目錄
BOOL res=[fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];`
if (res) {
NSLog(@"文件夾創(chuàng)建成功");
}else
NSLog(@"文件夾創(chuàng)建失敗");
}```
7,創(chuàng)建文件
//創(chuàng)建文件
-(void *)createFile{
NSString *documentsPath =[self dirDoc];
NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
BOOL res=[fileManager createFileAtPath:testPath contents:nil attributes:nil];
if (res) {
NSLog(@"文件創(chuàng)建成功: %@" ,testPath);
}else
NSLog(@"文件創(chuàng)建失敗");
}```
8,寫數(shù)據(jù)到文件
//寫文件
-(void)writeFile{
NSString *documentsPath =[self dirDoc];
NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
NSString *content=@"測試寫入內(nèi)容底循!";
BOOL res=[content writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
if (res) {
NSLog(@"文件寫入成功");
}else
NSLog(@"文件寫入失敗");
}```
9,讀文件數(shù)據(jù):
//讀文件
-(void)readFile{
NSString *documentsPath =[self dirDoc];
NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
// NSData *data = [NSData dataWithContentsOfFile:testPath];
// NSLog(@"文件讀取成功: %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
NSString *content=[NSString stringWithContentsOfFile:testPath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"文件讀取成功: %@",content);
}```
10,文件屬性:
//文件屬性
-(void)fileAttriutes{
NSString *documentsPath =[self dirDoc];
NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:testPath error:nil];
NSArray *keys;
id key, value;
keys = [fileAttributes allKeys];
int count = [keys count];
for (int i = 0; i < count; i++)
{
key = [keys objectAtIndex: i];
value = [fileAttributes objectForKey: key];
NSLog (@"Key: %@ for value: %@", key, value);
}
}```
11,刪除文件
//刪除文件
-(void)deleteFile{
NSString *documentsPath =[self dirDoc];
NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
BOOL res=[fileManager removeItemAtPath:testPath error:nil];
if (res) {
NSLog(@"文件刪除成功");
}else
NSLog(@"文件刪除失敗");
NSLog(@"文件是否存在: %@",[fileManager isExecutableFileAtPath:testPath]?@"YES":@"NO");
}