標籤: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)機制及擷取沙箱路徑