iOS開發(fā)關(guān)于沙盒

每個iOS程序都有一個獨立的文件系統(tǒng)(存儲空間)页慷,而且只能在對應(yīng)的文件系統(tǒng)中進行操作憔足,此區(qū)域被稱為沙盒。應(yīng)用必須待在自己的沙盒里酒繁,其他應(yīng)用不能訪問該沙盒滓彰。所有的非代碼文件都要保存在此,例如屬性文件plist州袒、文本文件揭绑、圖像、圖標(biāo)郎哭、媒體資源等他匪。

iOS app 沙盒目錄結(jié)構(gòu)

沙盒中相關(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ù):

  1. 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

};
  1. 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  // 上述所有及未來的位置
};
  1. BOOL expandTilde 表示是否用波浪線顯示部分目錄路徑婴削。~在*nix系統(tǒng)表示當(dāng)前用戶的Home目錄廊镜。列如上面獲取cache目錄路徑如果使用NO,那么結(jié)果就是cachesDir=~/Library/Caches

參考:
沙盒和NSBundle
NSSearchPathForDirectoriesInDomains方法使用
沙盒和NSBundle
文件系統(tǒng)官方手冊

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末唉俗,一起剝皮案震驚了整個濱河市嗤朴,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌虫溜,老刑警劉巖雹姊,帶你破解...
    沈念sama閱讀 216,372評論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異衡楞,居然都是意外死亡吱雏,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評論 3 392
  • 文/潘曉璐 我一進店門瘾境,熙熙樓的掌柜王于貴愁眉苦臉地迎上來歧杏,“玉大人,你說我怎么就攤上這事迷守∪蓿” “怎么了?”我有些...
    開封第一講書人閱讀 162,415評論 0 353
  • 文/不壞的土叔 我叫張陵兑凿,是天一觀的道長凯力。 經(jīng)常有香客問我,道長礼华,這世上最難降的妖魔是什么咐鹤? 我笑而不...
    開封第一講書人閱讀 58,157評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮卓嫂,結(jié)果婚禮上慷暂,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好行瑞,可當(dāng)我...
    茶點故事閱讀 67,171評論 6 388
  • 文/花漫 我一把揭開白布奸腺。 她就那樣靜靜地躺著,像睡著了一般血久。 火紅的嫁衣襯著肌膚如雪突照。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,125評論 1 297
  • 那天氧吐,我揣著相機與錄音讹蘑,去河邊找鬼。 笑死筑舅,一個胖子當(dāng)著我的面吹牛座慰,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播翠拣,決...
    沈念sama閱讀 40,028評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼版仔,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了误墓?” 一聲冷哼從身側(cè)響起蛮粮,我...
    開封第一講書人閱讀 38,887評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎谜慌,沒想到半個月后然想,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,310評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡欣范,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,533評論 2 332
  • 正文 我和宋清朗相戀三年变泄,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片熙卡。...
    茶點故事閱讀 39,690評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡杖刷,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出驳癌,到底是詐尸還是另有隱情滑燃,我是刑警寧澤,帶...
    沈念sama閱讀 35,411評論 5 343
  • 正文 年R本政府宣布颓鲜,位于F島的核電站表窘,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏甜滨。R本人自食惡果不足惜乐严,卻給世界環(huán)境...
    茶點故事閱讀 41,004評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望衣摩。 院中可真熱鬧昂验,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至甫恩,卻和暖如春逆济,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背磺箕。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評論 1 268
  • 我被黑心中介騙來泰國打工奖慌, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人松靡。 一個月前我還...
    沈念sama閱讀 47,693評論 2 368
  • 正文 我出身青樓简僧,卻偏偏與公主長得像,于是被迫代替她去往敵國和親击困。 傳聞我的和親對象是個殘疾皇子涎劈,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,577評論 2 353

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