iPhone 沙盒路徑

2021-06-06 12:28:37 字數 2800 閱讀 5623

documents:蘋果建議將程式中建立的或在程式中瀏覽到的檔案資料儲存在該目錄下;

library:儲存程式的預設設定或其它狀態資訊;

tmp:提供乙個即時建立臨時檔案的地方。

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

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

//取得documents路徑的方法:

- (nsstring *)documentfolder  

//取得documents中某個檔案的路徑

//獲取tmp目錄

nsstring

*temppath = nstemporarydirectory();

//補充:取得應用程式包(即bundle)的路徑

- (nsstring *)bundlefolder

一、沙盒(sandbox)

出於安全的目的,應用程式只能將自己的資料和偏好設定寫入到幾個特定的位置上。當應用程式被安裝到裝置上時,系統會為其建立乙個家目錄,這個家目錄就是應用程式的沙盒。

家目錄下共有四個子目錄:

documents 目錄:您應該將所有的應用程式資料檔案寫入到這個目錄下。這個目錄用於儲存使用者資料或其它應該定期備份的資訊。

library 目錄:這個目錄下有兩個子目錄:caches 和 preferences

preferences 目錄包含應用程式的偏好設定檔案。您不應該直接建立偏好設定檔案,而是應該使用nsuserdefaults類來取得和設定應用程式的偏好

caches 目錄用於存放應用程式專用的支援檔案,儲存應用程式再次啟動過程中需要的資訊。

tmp 目錄:這個目錄用於存放臨時檔案,儲存應用程式再次啟動過程中不需要的資訊。

獲取這些目錄路徑的方法:

//1,獲取家目錄路徑的函式:

nsstring *homedir = nshomedirectory(); 

//2,獲取documents目錄路徑的方法:

nsarray

*paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory,nsuserdomainmask,

yes);

nsstring *docdir = [paths 

objectatindex:0];

//3,獲取caches目錄路徑的方法:

nsarray

*paths = nssearchpathfordirectoriesindomains(nscachesdirectory,nsuserdomainmask,

yes);

nsstring *cachesdir = [paths 

objectatindex:0];

//4,獲取tmp目錄路徑的方法:

nsstring

*tmpdir = nstemporarydirectory();

//5,獲取應用程式程式包中資源檔案路徑的方法:

nsstring

*imagepath = [[

nsbundle

mainbundle]pathforresource:

oftype:

@"png"];

uiimage

uiimage

alloc]initwithcontentsoffile:

imagepath];

//**中的mainbundle類方法用於返回乙個代表應用程式包的物件。

二、檔案io

1,將資料寫到documents目錄:

nsarray

*paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory,nsuserdomainmask,

yes);

nsstring *docdir = [paths objectatindex:0];

if(!docdir)    

return [data writetofile:filepath atomically:yes];}

2,從documents目錄讀取資料:

nsarray

*paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory,nsuserdomainmask,

yes);

nsstring *docdir = [paths objectatindex:0];

nsdata

*data = [[[

nsdata 

alloc]initwithcontentsoffile:filepath]autorelease];

return data;}

全路徑:

iPhone 沙盒路徑

documents 蘋果建議將程式中建立的或在程式中瀏覽到的檔案資料儲存在該目錄下 library 儲存程式的預設設定或其它狀態資訊 tmp 提供乙個即時建立臨時檔案的地方。itunes在與iphone同步時,備份所有的documents和library檔案。iphone在重啟時,會丟棄所有的tmp...

iPhone 沙盒路徑

documents 蘋果建議將程式中建立的或在程式中瀏覽到的檔案資料儲存在該目錄下 library 儲存程式的預設設定或其它狀態資訊 tmp 提供乙個即時建立臨時檔案的地方。itunes在與iphone同步時,備份所有的documents和library檔案。iphone在重啟時,會丟棄所有的tmp...

開啟沙盒路徑

1 模擬器沙盒目錄 檔案都在個人使用者名稱資料夾下的乙個隱藏資料夾裡,中文叫資源庫,他的目錄其實是library。1.1 方法1 可以設定顯示隱藏檔案,然後在finder下直接開啟。設定檢視隱藏檔案的方法如下 開啟終端,輸入命名 顯示 mac隱藏檔案的命令 defaults write com.fi...