目錄
床前明月光刁俭,疑是地上霜隅津。 舉頭望明月,低頭思故鄉(xiāng)绣溜。
前言
1. 沙盒
當iOS系統(tǒng)安裝應用時库糠,會為該應用分配一塊獨立的空間(用來存放該應用的系統(tǒng)文件和緩存),該應用只能訪問該空間中的資源涮毫,這種機制稱之為沙盒機制瞬欧。
有4個文件夾:
1、應用名.app
應用程序包:程序本身罢防,包含資源艘虎。是經(jīng)過加簽的,運行時不能修改咒吐。
2野建、Documents
存儲用戶下載或保存的數(shù)據(jù)。會被iTunes備份恬叹。
3候生、Library
Caches文件夾:存儲SDWebImg...等緩存。不會被iTunes備份绽昼。
Preferences文件夾:會被iTunes備份唯鸭。存儲系統(tǒng)偏好設置NSUserdefaults。
4硅确、temp
存儲臨時文件目溉。不會被iTunes備份,應用銷毀后即清除菱农。
NSHomeDirectory() 沙盒根目錄
// 存儲路徑
NSString *path=[NSString stringWithFormat:@"%@/Documents/1.src",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();
/*
第一個參數(shù):想要查找的目錄
ypedef 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
};
第二個參數(shù):從哪個路徑區(qū)域查找
typedef NS_OPTIONS(NSUInteger, NSSearchPathDomainMask) {
NSUserDomainMask =1, // 用戶的主目錄
NSLocalDomainMask =2, // 當前機器的本地目錄
NSNetworkDomainMask =4, //在網(wǎng)絡中公開可用的位置
NSSystemDomainMask =8, // 被蘋果系統(tǒng)提供的缭付,不可更改的位置 (/System)
NSAllDomainsMask = 0x0ffff // 上述所有及未來的位置
};
第三個參數(shù):true時顯示具體路徑,false時使用~代替主目錄路徑循未。
*/
// [NSBundle mainBundle] 返回的路徑是 .../項目名.app/
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"apple"ofType:@"png"];
UIImage *appleImage = [[UIImage alloc] initWithContentsOfFile:imagePath];
2. 下載手機應用的沙盒目錄
當項目遇到保存錄音內(nèi)容等需求時陷猫,會需要查看應用沙盒目錄。
步驟
1. XCode的Window下點擊Devices and Simulators
2. 單擊installapp中的指定應用,點擊設置按鈕绣檬,點擊DownloadContainer
3. 找到下載的.xcappdata文件舅巷,右鍵查看包內(nèi)容
XCode的Window下點擊Devices and Simulators
單擊installapp中的指定應用,點擊設置按鈕河咽,點擊DownloadContainer
找到下載的xcap文件钠右,右鍵查看包內(nèi)容
Documents
Library