來感受下iOS app的沙盒
沙盒機(jī)制(sandBox)
iOS應(yīng)用程序只能在為該改程序創(chuàng)建的文件系統(tǒng)中讀取文件纠修,不可以去其它地方訪問胳嘲,此區(qū)域被稱為沙盒,所有的非代碼文件都要保存在此扣草,例如圖像了牛,圖標(biāo),聲音辰妙,映像鹰祸,屬性列表,文本文件等密浑。
- 每個(gè)應(yīng)用程序都有自己的存儲(chǔ)空間蛙婴。
- 應(yīng)用程序不能翻過自己的圍墻去訪問別的存儲(chǔ)空間的內(nèi)容。
- 應(yīng)用程序請(qǐng)求的數(shù)據(jù)都要通過權(quán)限檢測(cè)尔破,假如不符合條件的話街图,不會(huì)被放行。
如果要訪問到其他 App 的范圍懒构,必須要獲取管理員許可才行餐济,比如地理位置,相冊(cè)胆剧,通訊錄絮姆,話筒等。這是蘋果系統(tǒng)的哲學(xué),蘋果認(rèn)為只有把各個(gè) App 孤立起來才能營造良好的用戶體驗(yàn)和安全性篙悯。
通過下面的圖來看一下蘋果的沙盒機(jī)制吧.
通過這張圖只能從表層上理解sandbox是一種安全體系蚁阳,應(yīng)用程序的所有操作都要通過這個(gè)體系來執(zhí)行,其中核心內(nèi)容是:sandbox對(duì)應(yīng)用程序執(zhí)行各種操作的權(quán)限限制鸽照。
沙盒目錄結(jié)構(gòu)
默認(rèn)情況下螺捐,每個(gè)沙盒含有3個(gè)文件夾:Documents, Library 和 Tmp。因?yàn)閼?yīng)用的沙盒機(jī)制移宅,應(yīng)用只能在幾個(gè)目錄下讀寫文件
Documents:蘋果建議將程序中建立的或在程序中瀏覽到的文件數(shù)據(jù)保存在該目錄下归粉,iTunes備份和恢復(fù)的時(shí)候會(huì)包括此目錄椿疗。
Library:蘋果建議用來存放默認(rèn)設(shè)置或其它狀態(tài)信息漏峰。會(huì)被iTunes同步但是要除了Caches子目錄外。
Library/Caches:主要是緩存文件届榄,用戶使用過程中緩存都可以保存在這 個(gè)目錄中浅乔。這個(gè)目錄就用于保存那些可再生的文件,可以重新下載或者重新生成的數(shù)據(jù)應(yīng)該保存在目錄下面铝条。(比如雜志靖苇、新聞、地圖應(yīng)用使用的數(shù)據(jù)庫緩存文件和可下載內(nèi)容應(yīng)該保存到這個(gè)文件夾)班缰。磁盤空間不夠時(shí) 系統(tǒng)會(huì)刪除 不會(huì)被iTunes同步贤壁。
Library/Preferences:應(yīng)用程序的偏好設(shè)置文件。我們使用NSUserDefaults寫的設(shè)置數(shù)據(jù)都會(huì)保存到該目錄下的一個(gè)plist文件中埠忘,這就是所謂的寫道plist中脾拆! 會(huì)被iTunes同步。
tmp:各種臨時(shí)文件莹妒,保存應(yīng)用再次啟動(dòng)時(shí)不需要的文件名船。而且,當(dāng)應(yīng)用不再需要這些文件時(shí)應(yīng)該主動(dòng)將其刪除旨怠,因?yàn)樵撃夸浵碌臇|西隨時(shí)有可能被系統(tǒng)清理掉,也可能隨著項(xiàng)目退出刪掉渠驼。不會(huì)被iTunes同步。
iPhone在重啟時(shí)鉴腻,會(huì)丟棄所有的tmp文件迷扇。
沙盒文件的操作
獲取程序的home目錄
NSString *homeDirectory = NSHomeDirectory();
NSLog(@"path:%@", homeDirectory);
獲取document目錄
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSLog(@"path:%@", path);
獲取Cache目錄
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSLog(@"%@", path);
獲取Library目錄
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSLog(@"%@", path);
獲取Tmp目錄
NSString *tmpDir = NSTemporaryDirectory();
NSLog(@"%@", tmpDir);
存數(shù)據(jù)到沙盒plist文件中。
+(void)writeDataToPlist :(NSMutableArray *)dataArray
{
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path=[paths objectAtIndex:0];
NSLog(@"path:%@",path);
NSString *filename=[path stringByAppendingPathComponent:@"searchHistory.plist"];
//寫入
NSFileManager* fm = [NSFileManager defaultManager];
[fm createFileAtPath:filename contents:nil attributes:nil];
[dataArray writeToFile:filename atomically:YES];
}
沙盒plist文件讀取
+(NSArray *)getDataFormPlist{
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path=[paths objectAtIndex:0];
NSString *filename=[path stringByAppendingPathComponent:@"searchHistory.plist"];
//讀文件
NSArray* dataArray = [NSArray arrayWithContentsOfFile:filename];
return dataArray;
}
可以查看沙盒的應(yīng)用
文件常見操作
判斷某文件是否存在
NSFileManager* fm=[NSFileManager defaultManager];
if(![fm fileExistsAtPath:[self dataFilePath]]){
//下面是對(duì)該文件進(jìn)行制定路徑的保存
[fm createDirectoryAtPath:[self dataFilePath] withIntermediateDirectories:YES attributes:nil error:nil];
}
保存某個(gè)文件
可以用 NSFileManager的
- (BOOL)createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary *)attr;
或 NSData 的
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;
- (BOOL)writeToFile:(NSString *)path options:(NSUInteger)writeOptionsMask error:(NSError **)errorPtr;
刪除文件
NSFileManager* fm=[NSFileManager defaultManager];
NSError *myError = nil;
if ([fm removeItemAtPath:@"" error:&myError] == YES){
}
移動(dòng)文件
NSFileManager* fm=[NSFileManager defaultManager];
NSError *myError = nil;
if ([fm moveItemAtPath:@"" toPath:@"" error:&myError] == YES){
}
拷貝文件
NSFileManager* fm=[NSFileManager defaultManager];
NSError *myError = nil;
if ([fm copyItemAtPath:@"" toPath:@"" error:&myError] == YES){
}
取得一個(gè)目錄下得所有文件名
NSArray *files = [fm subpathsAtPath: [self dataFilePath] ];
讀取某個(gè)文件
NSData *data = [fm contentsAtPath:[self dataFilePath]];
//或者
NSData *data = [NSData dataWithContentOfPath:[self dataFilePath]];
保存漢字
NSString *fileName=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"myFile.txt"];
NSString *content=@"更深夜靜人已息";
NSData *contentData=[content dataUsingEncoding:NSUnicodeStringEncoding];
if ([contentData writeToFile:fileName atomically:YES]) {
NSLog(@">>write ok.");
}
使用工程中的文件
NSString *myFilePath = [[NSBundle mainBundle] pathForResource:@"f" ofType:@"txt"];
NSString *myFileContent=[NSString stringWithContentsOfFile:myFilePath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"bundel file path: %@ \nfile content:%@",myFilePath,myFileContent);
小結(jié)
后續(xù)如果有新的相關(guān)知識(shí)get爽哎,會(huì)持續(xù)更新蜓席。