每個iOS程序都有一個獨立的文件系統(tǒng)(存儲空間)页慷,而且只能在對應(yīng)的文件系統(tǒng)中進行操作憔足,此區(qū)域被稱為沙盒。應(yīng)用必須待在自己的沙盒里酒繁,其他應(yīng)用不能訪問該沙盒滓彰。所有的非代碼文件都要保存在此,例如屬性文件plist州袒、文本文件揭绑、圖像、圖標(biāo)郎哭、媒體資源等他匪。
沙盒中相關(guān)路徑
AppName.app
應(yīng)用程序的程序包目錄,包含應(yīng)用程序的本身彰居。由于應(yīng)用程序必須經(jīng)過簽名诚纸,所以不能在運行時對這個目錄中的內(nèi)容進行修改,否則會導(dǎo)致應(yīng)用程序無法啟動陈惰。Documents/
保存應(yīng)用程序的重要數(shù)據(jù)文件和用戶數(shù)據(jù)文件等畦徘。用戶數(shù)據(jù)基本上都放在這個位置(例如從網(wǎng)上下載的圖片或音樂文件),該文件夾在應(yīng)用程序更新時會自動備份抬闯,在連接iTunes時也可以自動同步備份其中的數(shù)據(jù)井辆。-
Library
:這個目錄下有兩個子目錄,可創(chuàng)建子文件夾∪芪眨可以用來放置您希望被備份但不希望被用戶看到的數(shù)據(jù)杯缺。該路徑下的文件夾,除Caches以外睡榆,都會被iTunes備份.Library/Caches
: 保存應(yīng)用程序使用時產(chǎn)生的支持文件和緩存文件(保存應(yīng)用程序再次啟動過程中需要的信息)萍肆,還有日志文件最好也放在這個目錄。iTunes 同步時不會備份該目錄并且可能被其他工具清理掉其中的數(shù)據(jù)胀屿。
Library/Preferences
: 保存應(yīng)用程序的偏好設(shè)置文件塘揣。NSUserDefaults
類創(chuàng)建的數(shù)據(jù)和plist
文件都放在這里。會被iTunes備份宿崭。 tmp/
: 保存應(yīng)用運行時所需要的臨時數(shù)據(jù)亲铡。不會被iTunes備份。iPhone重啟時,會被清空奖蔓。
// 獲取沙盒根目錄路徑
NSString *homeDir = NSHomeDirectory();
// 獲取Documents目錄路徑
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) firstObject];
//獲取Library的目錄路徑
NSString *libDir = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES) lastObject];
// 獲取cache目錄路徑
NSString *cachesDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES) firstObject];
// 獲取tmp目錄路徑
NSString *tmpDir =NSTemporaryDirectory();
// 獲取應(yīng)用程序程序包中資源文件路徑的方法:
NSString *bundle = [[NSBundle mainBundle] bundlePath];
NSLog(@"homeDir=%@ \n docDir=%@ \n libDir=%@ \n cachesDir=%@ \n tmpDir=%@ \n bundle=%@", homeDir,docDir, libDir, cachesDir, tmpDir, bundle);
結(jié)果為:
homeDir=/var/mobile/Containers/Data/Application/FBE41071-F5DD-4F02-9DFC-E9E473D3C917
docDir=/var/mobile/Containers/Data/Application/FBE41071-F5DD-4F02-9DFC-E9E473D3C917/Documents
libDir=/var/mobile/Containers/Data/Application/FBE41071-F5DD-4F02-9DFC-E9E473D3C917/Library
cachesDir=/var/mobile/Containers/Data/Application/FBE41071-F5DD-4F02-9DFC-E9E473D3C917/Library/Caches
tmpDir=/private/var/mobile/Containers/Data/Application/FBE41071-F5DD-4F02-9DFC-E9E473D3C917/tmp/
bundle=/var/containers/Bundle/Application/4A42EF08-30BA-4C19-B0C9-62903300C2BC/SandboxDemo.app
關(guān)于NSSearchPathForDirectoriesInDomains函數(shù)
FOUNDATION_EXPORT NSArray<NSString *> *NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory directory, NSSearchPathDomainMask domainMask, BOOL expandTilde);
用于查找目錄赞草,返回指定范圍內(nèi)的指定名稱的目錄的路徑集合。有三個參數(shù):
-
NSSearchPathDirectory directory
想要查找的目錄吆鹤,是個枚舉值厨疙,有很多值,有關(guān)于iOS的檀头,有關(guān)于macOS轰异,也有關(guān)于watchOS的。
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 NS_ENUM_AVAILABLE(10_8, NA) = 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
};
-
NSSearchPathDomainMask domainMask
表示“想要從哪個路徑區(qū)域保護區(qū)查找”暑始。
typedef NS_OPTIONS(NSUInteger, NSSearchPathDomainMask) {
NSUserDomainMask =1, // 用戶的主目錄
NSLocalDomainMask =2, // 當(dāng)前機器的本地目錄
NSNetworkDomainMask =4, //在網(wǎng)絡(luò)中公開可用的位置
NSSystemDomainMask =8, // 被蘋果系統(tǒng)提供的,不可更改的位置 (/System)
NSAllDomainsMask = 0x0ffff // 上述所有及未來的位置
};
-
BOOL expandTilde
表示是否用波浪線顯示部分目錄路徑婴削。~
在*nix系統(tǒng)表示當(dāng)前用戶的Home目錄廊镜。列如上面獲取cache目錄路徑如果使用NO,那么結(jié)果就是cachesDir=~/Library/Caches
參考:
沙盒和NSBundle
NSSearchPathForDirectoriesInDomains方法使用
沙盒和NSBundle
文件系統(tǒng)官方手冊