iphone沙箱模型的有四個文件夾,分別是什么,永久數(shù)據(jù)存儲一般放在什么位置,得到模擬器的路徑的簡單方式是什么.
documents,tmp,app,Library。
(NSHomeDirectory()),
手動保存的文件在documents文件里
Nsuserdefaults保存的文件在tmp文件夾里
1、Documents 目錄:您應(yīng)該將所有de應(yīng)用程序數(shù)據(jù)文件寫入到這個目錄下。這個目錄用于存儲用戶數(shù)據(jù)或其它應(yīng)該定期備份的信息。
2、AppName.app 目錄:這是應(yīng)用程序的程序包目錄,包含應(yīng)用程序的本身。由于應(yīng)用程序必須經(jīng)過簽名,所以您在運行時不能對這個目錄中的內(nèi)容進行修改,否則可能會使應(yīng)用程序無法啟動。
3、Library 目錄:這個目錄下有兩個子目錄:Caches 和 Preferences
Preferences 目錄:包含應(yīng)用程序的偏好設(shè)置文件。您不應(yīng)該直接創(chuàng)建偏好設(shè)置文件,而是應(yīng)該使用NSUserDefaults類來取得和設(shè)置應(yīng)用程序的偏好.
Caches 目錄:用于存放應(yīng)用程序?qū)S玫闹С治募?,保存?yīng)用程序再次啟動過程中需要的信息。
4、tmp 目錄:這個目錄用于存放臨時文件,保存應(yīng)用程序再次啟動過程中不需要的信息。
獲取這些目錄路徑的方法:
1,獲取家目錄路徑的函數(shù):
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,獲取應(yīng)用程序程序包中資源文件路徑的方法:
例如獲取程序包中一個圖片資源(apple.png)路徑的方法:
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@”apple” ofType:@”png”];
UIImage *appleImage = [[UIImage alloc] initWithContentsOfFile:imagePath];
代碼中的mainBundle類方法用于返回一個代表應(yīng)用程序包的對象。
iphone沙盒(sandbox)中的幾個目錄獲取方式:
[cpp]view plaincopy
//?獲取沙盒主目錄路徑
NSString?*homeDir?=?NSHomeDirectory();
//?獲取Documents目錄路徑
NSArray?*paths?=?NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,?NSUserDomainMask,?YES);
NSString?*docDir?=?[paths?objectAtIndex:0];
//?獲取Caches目錄路徑
NSArray?*paths?=?NSSearchPathForDirectoriesInDomains(NSCachesDirectory,?NSUserDomainMask,?YES);
NSString?*cachesDir?=?[paths?objectAtIndex:0];
//?獲取tmp目錄路徑
NSString?*tmpDir?=??NSTemporaryDirectory();
[cpp]view plaincopy
//?獲取當(dāng)前程序包中一個圖片資源(apple.png)路徑
NSString?*imagePath?=?[[NSBundle?mainBundle]?pathForResource:@"apple"ofType:@"png"];
UIImage?*appleImage?=?[[UIImage?alloc]?initWithContentsOfFile:imagePath];
例子:
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]];
}