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

來源:互聯網
上載者:User

標籤:style   blog   color   io   os   ar   for   sp   檔案   

一、每個iOS應用SDK都被限制在沙箱中,沙箱相當於一個加了僅主人可見許可權的檔案夾,蘋果對沙箱有以下幾條限制。

(1)、應用程式可以在自己的沙箱裡運作,但是不能訪問任何其他應用程式的沙箱。

(2)、應用程式間不能共用資料,沙箱裡的檔案不能被複製到其他應用程式檔案夾中,也不能把其他應用程式檔案夾中的檔案複製到沙箱裡。

(3)、蘋果禁止任何讀、寫沙箱以外的檔案,禁止應用程式將內容寫到沙箱以外的檔案夾中。

(4)、沙箱根目錄裡有三個檔案夾:

  Documents:一般應該把應用程式的資料檔案存到這個檔案夾裡,用於儲存使用者資料或其他應該定期備份的資訊。

  Library:下有兩個檔案夾,Caches儲存應用程式再次啟動所需的資訊(快取檔案等),Preferences包含應用程式喜好設定檔案,不過不要在這裡修改喜好設定。

  Temp:存放臨時檔案,即應用再次啟動不需要的檔案。

二、擷取沙箱路徑

(1)、擷取沙箱根目錄有以下幾種方法:

  a、用NSHomeDirectory擷取

1 NSString *path = NSHomeDirectory();2 NSLog(@"path = %@",path);

  b、用使用者名稱擷取

1 NSString *userName = NSUserName();// 擷取建立該應用程式的使用者名稱2 NSString *rootPath = NSHomeDirectoryForUser(userName);3 NSLog(@"rootPath = %@",rootPath);

  c、擷取Documents路徑

1 NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];2 NSString *filePath = [documentPath stringByAppendingString:@"data.plist"];//向Documents裡添加檔案3 NSLog(@"filePath = %@",filePath);

  d、擷取temp路徑

1 NSString *tempPath = NSTemporaryDirectory();// 擷取temp目錄,如果在裡面寫資料,當程式退出會清空2 NSLog(@"tempPath = %@",tempPath);

  e、擷取caches路徑

1 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);2 NSString *cachesPath = [paths objectAtIndex:0];3 NSLog(@"cachesPath = %@",cachesPath);

 

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.