IOS沙盒Files目錄說明和常用操作

Ios沙盒目錄清單

1 Documents

用于存儲用戶生成的文件、其他數(shù)據(jù)及其他程序不能重新創(chuàng)建的文件,默認文件通過iCloud自動備份。如果不需要iCloud備份,則設置標記

NSURLIsExcludedFromBackupKey,注明不備份

1.1 讀取Documents目錄代碼

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

NSString*path = [paths objectAtIndex:0];

NSLog(@"path:%@", path);

1.2 標記不備份文件

+ (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL

{

assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);

NSError*error =nil;

BOOL success=[URL setResourceValue: [NSNumber numberWithBool: YES]

forKey: NSURLIsExcludedFromBackupKey error:&error];if(!success){

NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);

}returnsuccess;

}

2.Library

存儲項目緩存,常用設置等

2.1 讀取Cache目錄代碼

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

NSString*path = [paths objectAtIndex:0];

NSLog(@"%@", path);

2.2 讀取Library目錄

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

NSString*path = [paths objectAtIndex:0];

NSLog(@"%@", path);

3 temp

只是臨時使用的數(shù)據(jù)應該保存到?/tmp?文件夾。盡管?iCloud?不會備份這些文件,但在應用在使用完這些數(shù)據(jù)之后要注意隨時刪除,避免占用用戶設備的空間

3.1讀取temp文件夾

NSString *tmpDir =NSTemporaryDirectory();

NSLog(@"%@", tmpDir);

4 項目自帶的.bundle資源束

項目自帶的資源存放在.bundle中這些資源只讀不能寫

NSString*defaultDBPath = [[NSBundlemainBundle]resourcePath];

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容