iOS沙盒路徑的檢視和使用

2021-07-04 20:36:43 字數 2085 閱讀 3840

1、模擬器沙盒目錄

檔案都在個人使用者名稱資料夾下的乙個隱藏資料夾裡,中文叫資源庫,他的目錄其實是library。

因為應用是在沙箱(sandbox)中的,在檔案讀寫許可權上受到限制,只能在幾個目錄下讀寫檔案:

documents:應用中使用者資料可以放在這裡,itunes備份和恢復的時候會包括此目錄

tmp:存放臨時檔案,itunes不會備份和恢復此目錄,此目錄下檔案可能會在應用退出後刪除

library/caches:存放快取檔案,itunes不會備份此目錄,此目錄下檔案不會在應用退出刪除

itunes在與iphone同步時,備份所有的documents和library檔案。

iphone在重啟時,會丟棄所有的tmp檔案。

檢視方法:

方法1、可以設定顯示隱藏檔案,然後在finder下直接開啟。設定檢視隱藏檔案的方法如下:開啟終端,輸入命名

(3)輸完單擊enter鍵,退出終端,重新啟動finder就可以了 重啟finder:滑鼠單擊視窗左上角的蘋果標誌-->強制退出-->finder-->

現在能看到資源庫資料夾了。 

username這裡寫使用者名稱。 

**檢視目錄:

nsstring *path = nshomedirectory();//

主目錄 nslog(@"

nshomedirectory:%@

",path);

nsstring *username = nsusername();//

與上面相同

nsstring *rootpath =nshomedirectoryforuser(username);

nslog(

@"nshomedirectoryforuser:%@

",rootpath);

nsarray *paths =nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes);

nsstring *documentsdirectory=[paths objectatindex:0];//

documents目錄

nslog(@"

nsdocumentdirectory:%@

",documentsdirectory);

結果如下:

2013-09-03

2013-09-03

2013-09-03

自定義類返回各目錄路徑:

#import

@inte***ce

icsandboxhelper : nsobject

+ (nsstring *)homepath; //

程式主目錄,可見子目錄(3個):documents、library、tmp

程式目錄,不能存任何東西

+ (nsstring *)docpath; //

文件目錄,需要itunes同步備份的資料存這裡,可存放使用者資料

+ (nsstring *)libprefpath; //

配置目錄,配置檔案存這裡

+ (nsstring *)libcachepath; //

快取目錄,系統永遠不會刪除這裡的檔案,itunes會刪除

+ (nsstring *)tmppath; //

+ (bool)haslive:(nsstring *)path; //

判斷目錄是否存在,不存在則建立

#import

"icsandboxhelper.h

"@implementation

icsandboxhelper

+ (nsstring *)homepath

+ (nsstring *)docpath

+ (nsstring *)libprefpath

+ (nsstring *)libcachepath

+ (nsstring *)tmppath

+ (bool)haslive:(nsstring *)path

return

no;}

iOS沙盒路徑的檢視和使用

1 模擬器沙盒目錄 檔案都在個人使用者名稱資料夾下的乙個隱藏資料夾裡,中文叫資源庫,他的目錄其實是library。因為應用是在沙箱 sandbox 中的,在檔案讀寫許可權上受到限制,只能在幾個目錄下讀寫檔案 documents 應用中使用者資料可以放在這裡,itunes備份和恢復的時候會包括此目錄 ...

iOS沙盒路徑的檢視和使用

獲取這些目錄路徑的方法 1,獲取主目錄路徑的函式 nsstring homedir nshomedirectory 2,獲取documents目錄路徑的方法 nsarray paths nssearchpathfordirectoriesindomains nsdocumentdirectory,n...

iOS沙盒路徑

ios沙河路徑極其作用 documents 儲存使用者資料,itunes備份會包括此目錄下資料。library 包含兩個子目錄 caches 和 preferences。caches 存放使用者需要快取的資料 快取需要有清理操作 tmp 用於存放臨時檔案,此目錄下檔案可能會在應用退出後刪除。檢視路徑...