1碱鳞、沙河盒根目錄的結(jié)構(gòu)
- Documents:此目錄用來保存應(yīng)用程序在運行時生成的一些需要長久保存的重要數(shù)據(jù)放在此文件中。通過iTunes吃嘿,iCloud備份時最仑,會備份此目錄下的數(shù)據(jù)。iTunes共享文件時赏壹,可以共享此文件目錄鱼炒。
- Library
- Caches :此目錄用來保存應(yīng)用程序運行時生成的需要持久化的數(shù)據(jù),這些數(shù)據(jù)一般存儲體積比較大蝌借,又不是十分重要田柔,比如網(wǎng)絡(luò)請求數(shù)據(jù)等。這些數(shù)據(jù)需要用戶負責(zé)刪除骨望。iTunes同步設(shè)備時不會備份該目錄硬爆。
- Preferences :此目錄保存應(yīng)用程序的所有偏好設(shè)置,iOS的Settings(設(shè)置)應(yīng)用會在該目錄中查找應(yīng)用的設(shè)置信息擎鸠。在Preferences/下不能直接創(chuàng)建偏好設(shè)置文件缀磕,而是應(yīng)該使用NSUserDefaults類來取得和設(shè)置應(yīng)用程序的偏好。iTunes同步設(shè)備時會備份該目錄。
-
tmp:此目錄保存應(yīng)用程序運行時所需的臨時數(shù)據(jù)袜蚕,使用完畢后再將相應(yīng)的文件從該目錄刪除糟把。應(yīng)用沒有運行時,系統(tǒng)也可能會清除該目錄下的文件牲剃。iTunes同步設(shè)備時不會備份該目錄遣疯。
image
2、獲取各種文件目錄的路徑
- 獲取目錄路徑的方法:
// 獲取沙盒主目錄路徑
NSString *homeDir = NSHomeDirectory();
// 獲取Documents目錄路徑
// NSString *docDir = [homeDir stringByAppendingPathComponent:@"Documents"];
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
// 獲取Library的目錄路徑
NSString *libDir = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
// 獲取Caches目錄路徑
NSString *cachesDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
// 獲取tmp目錄路徑
NSString *tmpDir = NSTemporaryDirectory();
- 獲取應(yīng)用程序程序包中資源文件路徑的方法:
NSLog(@"%@", [[NSBundle mainBundle] bundlePath]);
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"apple" ofType:@"png"];
UIImage *appleImage = [[UIImage alloc] initWithContentsOfFile:imagePath];
3凿傅、獲取模擬器沙盒文件
使用斷點缠犀,在控制臺選中輸入:po NSHomeDirectory()
。
4聪舒、獲取真機沙盒文件
- 連接設(shè)備辨液,在Xcode下點擊Window->Device and Simulators(cmd + shift + 2) 彈出窗口,左邊選擇你的設(shè)備箱残,右邊找到你已安裝的APP滔迈,選中你想要查看沙盒文件的APP。
- 點擊底部有個類似設(shè)置的按鈕被辑,出現(xiàn)幾個選項燎悍,選擇Download Container ,下載文件到本地盼理,將會看到一個后綴為xcappdata的文件谈山,選擇這個文件并顯示包內(nèi)容查看沙盒文件。
5:macOS文件(目錄)名不區(qū)分大小寫榜揖,但是iOS有區(qū)分勾哩,創(chuàng)建目錄在真機和模擬器下會有不同結(jié)果
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *testPath = [docPath stringByAppendingPathComponent:@"test"];
NSString *TestPath = [docPath stringByAppendingPathComponent:@"Test"];
BOOL isDir;
NSError *error = nil;
if ([fileManager fileExistsAtPath:testPath isDirectory:&isDir] && isDir) {
NSLog(@"test目錄已存在");
} else {
[fileManager createDirectoryAtPath:testPath withIntermediateDirectories:YES attributes:nil error:&error];
if (error) {
NSLog(@"創(chuàng)建test目錄失敗:%@", error);
} else {
NSLog(@"創(chuàng)建test目錄成功");
}
}
if ([fileManager fileExistsAtPath:TestPath isDirectory:&isDir] && isDir) {
NSLog(@"Test目錄已存在");
} else {
[fileManager createDirectoryAtPath:TestPath withIntermediateDirectories:YES attributes:nil error:&error];
if (error) {
NSLog(@"創(chuàng)建Test目錄失敗:%@", error);
} else {
NSLog(@"創(chuàng)建Test目錄成功");
}
}
//macOS文件名不區(qū)分大小寫抗蠢,但iOS區(qū)分大小寫
/*模擬器下輸出:
test目錄已存在
創(chuàng)建Test目錄失敗:Error Domain=NSCocoaErrorDomain Code=4 "The file “Test” doesn’t exist." UserInfo={NSFilePath=/Users/admin/Library/Developer/CoreSimulator/Devices/10DFAD8C-74DB-4129-8C90-2991A89C5574/data/Containers/Data/Application/513F18A5-C444-4824-AC4A-046230ED1498/Documents/Test, NSUnderlyingError=0x6000000836f0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
*/
/*真機下輸出:
創(chuàng)建test目錄成功
創(chuàng)建Test目錄成功
*/