某個(gè)iphone工程進(jìn)行文件操作有此工程對(duì)應(yīng)的指定的位置,不能逾越。
iphone沙箱模型的有四個(gè)文件夾短条,分別是什么拢操,永久數(shù)據(jù)存儲(chǔ)一般放在什么位置,得到模擬器的路徑的簡(jiǎn)單方式是什么.
documents,tmp,app,Library甲雅。
(NSHomeDirectory()),
手動(dòng)保存的文件在documents文件里
Nsuserdefaults保存的文件在tmp文件夾里
Documents 目錄:您應(yīng)該將所有de應(yīng)用程序數(shù)據(jù)文件寫入到這個(gè)目錄下坑填。這個(gè)目錄用于存儲(chǔ)用戶數(shù)據(jù)或其它應(yīng)該定期備份的信息抛人。
AppName.app 目錄:這是應(yīng)用程序的程序包目錄,包含應(yīng)用程序的本身脐瑰。由于應(yīng)用程序必須經(jīng)過簽名妖枚,
所以您在運(yùn)行時(shí)不能對(duì)這個(gè)目錄中的內(nèi)容進(jìn)行修改,否則可能會(huì)使應(yīng)用程序無法啟動(dòng)苍在。
Library 目錄:這個(gè)目錄下有兩個(gè)子目錄:Caches 和 Preferences
Preferences 目錄包含應(yīng)用程序的偏好設(shè)置文件绝页。您不應(yīng)該直接創(chuàng)建偏好設(shè)置文件,而是應(yīng)該使用NSUserDefaults類來取得和設(shè)置應(yīng)用程序的偏好.
Caches 目錄用于存放應(yīng)用程序?qū)S玫闹С治募盘瘢4鎽?yīng)用程序再次啟動(dòng)過程中需要的信息续誉。
tmp 目錄:這個(gè)目錄用于存放臨時(shí)文件,保存應(yīng)用程序再次啟動(dò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)用程序程序包中資源文件路徑的方法:
例如獲取程序包中一個(gè)圖片資源(apple.png)路徑的方法:
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@”apple” ofType:@”png”];
UIImage *appleImage = [[UIImage alloc] initWithContentsOfFile:imagePath];
代碼中的mainBundle類方法用于返回一個(gè)代表應(yīng)用程序包的對(duì)象眠寿。
文件IO寫入
1,將數(shù)據(jù)寫到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目錄讀取數(shù)據(jù):
- (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這個(gè)主要就是返回一個(gè)絕對(duì)路徑用來存放我們需要儲(chǔ)存的文件盯拱。
- (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]]){
//下面是對(duì)該文件進(jìn)行制定路徑的保存
[fm createDirectoryAtPath:[self dataFilePath] withIntermediateDirectories:YES attributes:nil error:nil];
//取得一個(gè)目錄下得所有文件名
NSArray *files = [fm subpathsAtPath: [self dataFilePath] ];
//讀取某個(gè)文件
NSData *data = [fm contentsAtPath:[self dataFilePath]];
//或者
NSData *data = [NSData dataWithContentOfPath:[self dataFilePath]];
}
iphone常見私有API的應(yīng)用(比如直接發(fā)送短信,訪問沙箱之外的磁盤文件)