IOS之sandbox

來源:互聯網
上載者:User

原文:http://blog.csdn.net/liliangchw/article/details/8314181

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

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

Documents 目錄:您應該將所有的應用程式資料檔案寫入到這個目錄下。這個目錄用於儲存使用者資料或其它應該定期備份的資訊。
AppName.app 目錄:這是應用程式的程式包目錄,包含應用程式的本身。由於應用程式必須經過簽名,所以您在運行時不能對這個目錄中的內容進行修改,否則可能會使應用程式無法啟動。
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,擷取應用程式程式包中資源檔路徑的方法:
例如擷取程式包中一個圖片資源(apple.png)路徑的方法:
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@”apple” ofType:@”png”];
UIImage *appleImage = [[UIImage alloc] initWithContentsOfFile:imagePath];

代碼中的mainBundle類方法用於返回一個代表應用程式套件組合的對象。

二、檔案IO
1,將資料寫到Documents目錄:
- (BOOL)writeApplicationData:(NSData *)data toFile:(NSString *)fileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *docDir = [paths objectAtIndex:0]; if (!docDir) { NSLog(@”Documents directory not found!”); return NO; } NSString *filePath = [docDir stringByAppendingPathComponent:fileName];
return [data writeToFile:filePath atomically:YES];}

2,從Documents目錄讀取資料:
- (NSData *)applicationDataFromFile:(NSString *)fileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *docDir = [paths objectAtIndex:0]; NSString *filePath = [docDir stringByAppendingPathComponent:fileName]; NSData *data = [[[NSData alloc] initWithContentsOfFile:filePath]
autorelease]; return data;}

NSSearchPathForDirectoriesInDomains這個主要就是返回一個絕對路徑用來存放我們需要儲存的檔案。

- (NSString *)dataFilePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:@"shoppingCar.plist"];
}

NSFileManager* fm=[NSFileManager defaultManager];
if(![fm fileExistsAtPath:[self dataFilePath]]){
//下面是對該檔案進行制定路徑的儲存
[fm createDirectoryAtPath:[self dataFilePath] withIntermediateDirectories:YES attributes:nil error:nil];
//取得一個目錄下得所有檔案名稱
NSArray *files = [fm subpathsAtPath: [self dataFilePath] ];
//讀取某個檔案
NSData *data = [fm contentsAtPath:[self dataFilePath]];
//或者
NSData *data = [NSData dataWithContentOfPath:[self dataFilePath]];
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.