預設情況下,每個沙盒含有3個檔案夾:Documents, Library 和 tmp。
Documents:蘋果建議将程式中建立的或在程式中浏覽到的檔案資料儲存在該目錄下;
Library:存儲程式的預設設定或其它狀态資訊;
tmp:提供一個即時建立臨時檔案的地方。
iTunes在與iPhone同步時,備份所有的Documents和Library檔案。
iPhone在重新開機時,會丢棄所有的tmp檔案。
//取得Documents路徑的方法:
- (NSString *)documentFolder {
return [NSHomeDirectory()stringByAppendingPathComponent:@"Documents"];
}
//取得Documents中某個檔案的路徑
NSString *path = [[self documentFolder] stringByAppendingPathComponent:@"image.png"];
//擷取tmp目錄
NSString *tempPath = NSTemporaryDirectory();
//補充:取得應用程式包(即bundle)的路徑
- (NSString *)bundleFolder {
return [[NSBundlemainBundle]bundlePath];
}
一、沙盒(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 = [[NSBundlemainBundle]pathForResource:@"apple"ofType:@"png"];
UIImage *appleImage = [[UIImagealloc]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;
}
全路徑:
/Users/XZY/Library/Application Support/iPhone Simulator/5.1
/Applications/401419FF-7B91-4262-AB68-E3AF695D8310/Library/Preferences
- (NSData *)applicationDataFromFile:(NSString *)fileName {
NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *filePath = [docDir stringByAppendingPathComponent:fileName];
NSData *data = [[[NSDataalloc]initWithContentsOfFile:filePath]autorelease];
return data;
}
?