iOS沙箱(sandbox)機制及擷取沙箱路徑

來源:互聯網
上載者:User

標籤:ios   移動互連網   sandbox   沙箱   

一、什麼是sandbox     每個iOS應用都被限制在“沙箱”中,“沙箱”相當於一個加了僅主人可見許可權的檔案夾,蘋果對沙箱主要有以下限制。     1、應用程式可以在自己的沙箱裡運作,但是不能訪問任何其他應用程式的沙     2、應用程式間不能共用資料,沙箱裡的檔案不能被複製到其他應用程式檔案夾中,也不能把其他應用程式檔案夾中的檔案複製到沙箱裡。     3、蘋果禁止任何讀、寫沙箱以外的檔案,禁止應用程式將內容寫到沙箱以外的檔案夾中。     4、 iOS應用程式的沙箱中包括三個檔案夾:
  • Documents
    Apple官方建議將程式中建立的或程式瀏覽的檔案資料都儲存在Documents目錄裡,itunes備份資料時或恢複資料時,會備份或恢複此目錄;
  • Library
    主要儲存的是預設的設定或其它的狀態資訊。包含以下兩個檔案夾:
    • Caches
      儲存快取檔案,儲存應用程式再次啟動所需的,itunes不會備份該目錄;
    • Preferences
      儲存應用程式喜好設定檔案,一般不修改這裡存放的檔案;
  • tmp
    提供的是一個儲存臨時檔案的地方。iPhone一旦重啟tmp目錄下的內容就會被清空。

二、如何擷取沙箱路徑     1、擷取沙箱根目錄     (1)、通過NSString*NSHomeDirectory(void)直接擷取    NSString *homePath =NSHomeDirectory();   (2)、通過NSString*NSHomeDirectoryForUser(NSString*userName)擷取    NSString *userName =NSUserName();    NSString *homePath2 = NSHomeDirectoryForUser(userName);     2、擷取沙箱其他檔案路徑     Documents、Library、Caches通過以下方法進行搜尋:    NSArray*NSSearchPathForDirectoriesInDomains(NSSearchPathDirectorydirectory, NSSearchPathDomainMask domainMask,BOOLexpandTilde);     NSSearchPathDirectory,枚舉值,制定搜尋沙箱中的子目錄     NSSearchPathDomainMask, 枚舉值,指定搜尋範圍         expandTilde,是否顯示全路徑
  • 擷取Documents路徑
    NSArray *arrayOfDocPath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);    NSString *docPath = [arrayOfDocPath objectAtIndex:0];
  • 擷取Library路徑
    NSArray *arrayOfLibPath =NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES);    NSString *libPath = [arrayOfLibPath objectAtIndex:0];
    • 擷取Caches路徑
    NSArray *arrayOfCache =NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);    NSString *cachesPath = [arrayOfCache objectAtIndex:0];
    • 擷取Preferences路徑
    NSString *prePath = [libPathstringByAppendingPathComponent:@"Preferences"];//通過Library路徑拼接上檔案夾名,一般不會讀取、修改該檔案夾下的檔案
  • 擷取tmp路徑
    NSString *temPath =NSTemporaryDirectory();
  • 擷取app目錄
    NSString *bundlePath = [[NSBundlemainBundle]bundlePath];
  • 擷取app中資源檔路徑
    NSString *imgPath = [[NSBundlemainBundle]pathForResource:@"01loading.png"ofType:nil];//該方法有多個重載

iOS沙箱(sandbox)機制及擷取沙箱路徑

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.