iOS數(shù)據(jù)存儲(chǔ)之文件沙盒

資源連接:

iOS數(shù)據(jù)庫存儲(chǔ)之SQL語句;

iOS數(shù)據(jù)庫存儲(chǔ)之SQLite3;

iOS數(shù)據(jù)存儲(chǔ)之NSCoding;

iOS文件沙盒簡(jiǎn)介

Note:iOS中的沙盒機(jī)制(SandBox)是一種安全體系,它規(guī)定了應(yīng)用程序只能在為該應(yīng)用創(chuàng)建的文件夾內(nèi)讀取文件,不可以訪問其他地方的內(nèi)容。所有的非代碼文件都保存在這個(gè)地方,比如圖片、聲音、屬性列表和文本文件等。

  1. Sandbox (computer security), a virtual container in which untrusted programs can be safely run
  1. Sandbox (software development), an online environment in which code or content changes can be tested without affecting the original system

  2. 每個(gè)應(yīng)用程序都在自己的沙盒內(nèi)。

  3. 不能隨意跨越自己的沙盒去訪問別的應(yīng)用程序沙盒的內(nèi)容。

  4. 應(yīng)用程序向外請(qǐng)求或接收數(shù)據(jù)都需要經(jīng)過權(quán)限認(rèn)證(從其他沙盒)。

應(yīng)用沙盒一般包括以下幾個(gè)文件目錄:應(yīng)用程序包、Documents、Libaray(下面有Caches和Preferences目錄)、tmp。

應(yīng)用沙盒目錄

  1. 應(yīng)用程序包:包含所有的資源文件和可執(zhí)行文件。

    Note:路徑,[[NSBundle mainBundle] bundlePath];

  2. 沙盒路徑

    NSHomeDirectory()
    
  3. Documents:保存應(yīng)用運(yùn)行時(shí)生成的需要持久化的數(shù)據(jù),iTunes會(huì)自動(dòng)備份該目錄。蘋果建議將程序中建立的或在程序中瀏覽到的文件數(shù)據(jù)保存在該目錄下,iTunes備份和恢復(fù)的時(shí)候會(huì)包括此目錄。

    NSArray *arrDocumentPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
     
     NSString *documentPath=[arrDocumentPaths objectAtIndex:0];
     NSLog(@"Documents path: %@",documentPath);
    
  4. tmp:保存應(yīng)用運(yùn)行時(shí)所需的臨時(shí)數(shù)據(jù),使用完畢后再將相應(yīng)的文件從該目錄刪除。應(yīng)用沒有運(yùn)行時(shí),系統(tǒng)也有可能會(huì)清除該目錄下的文件,iTunes不會(huì)同步該目錄。iphone重啟時(shí),該目錄下的文件會(huì)丟失。

    NSTemporaryDirectory()
    
  5. Libaray/Caches:存放緩存文件,iTunes不會(huì)備份此目錄,此目錄下文件不會(huì)在應(yīng)用退出刪除。一般存放體積比較大,不是特別重要的資源。

    //Library目錄  
    NSArray *libsPath =  NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);  
    NSString *libPath = [libsPath objectAtIndex:0];  
    

NSLog(@"Library目錄:%@",libPath);

NSArray *arrCachesPaths=NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);
NSString *CachesPath=[arrCachesPaths objectAtIndex:0];
NSLog(@"Caches path: %@",CachesPath);
```
  1. Libaray/Preferences:保存應(yīng)用的所有偏好設(shè)置,iOS的Settings(設(shè)置)應(yīng)用會(huì)在該目錄中查找應(yīng)用的設(shè)置信息,iTunes會(huì)自動(dòng)備份該目錄。
用戶偏好設(shè)置,plist格式文檔;
系統(tǒng)提供[NSUserDefaults standardUserDefaults]單例類直接操作文檔。

應(yīng)用程序包和沙盒的路徑區(qū)別

沙盒和應(yīng)用包.png

工具類

  1. FileUtils.h
#import <Foundation/Foundation.h>

@interface FileUtils : NSObject

//返回緩存根目錄 "caches"
+(NSString *)getCachesDirectory;

//返回根目錄路徑 "document"
+ (NSString *)getDocumentPath;

//創(chuàng)建文件夾
+(BOOL)creatDir:(NSString*)dirPath;

//刪除文件夾
+(BOOL)deleteDir:(NSString*)dirPath;

//移動(dòng)文件夾
+(BOOL)moveDir:(NSString*)srcPath to:(NSString*)desPath;

//創(chuàng)建文件
+ (BOOL)creatFile:(NSString*)filePath withData:(NSData*)data;

//讀取文件
+(NSData*)readFile:(NSString *)filePath;

//刪除文件
+(BOOL)deleteFile:(NSString *)filePath;

//返回 文件全路徑
+ (NSString*)getFilePath:(NSString*) fileName;

//在對(duì)應(yīng)文件保存數(shù)據(jù)
+ (BOOL)writeDataToFile:(NSString*)fileName data:(NSData*)data;

//從對(duì)應(yīng)的文件讀取數(shù)據(jù)
+ (NSData*)readDataFromFile:(NSString*)fileName;

@end

  1. FileUtils.m
#import "FileUtils.h"

@implementation FileUtils

//返回緩存根目錄 "caches"
+(NSString *)getCachesDirectory
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *caches = [paths firstObject];
    return caches;
}

//返回根目錄路徑 "document"
+ (NSString *)getDocumentPath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [paths firstObject];
    return documentPath;
}

//創(chuàng)建文件目錄
+(BOOL)creatDir:(NSString*)dirPath
{
    if ([[NSFileManager defaultManager] fileExistsAtPath:dirPath])//判斷dirPath路徑文件夾是否已存在,此處dirPath為需要新建的文件夾的絕對(duì)路徑
    {
        return NO;
    }
    else
    {
        [[NSFileManager defaultManager] createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:nil];//創(chuàng)建文件夾
        return YES;
    }
    
}

//刪除文件目錄
+(BOOL)deleteDir:(NSString*)dirPath
{
    if([[NSFileManager defaultManager] fileExistsAtPath:dirPath])//如果存在臨時(shí)文件的配置文件
        
    {
        NSError *error=nil;
         return [[NSFileManager defaultManager]  removeItemAtPath:dirPath error:&error];
        
    }  

    return  NO;
}

//移動(dòng)文件夾
+(BOOL)moveDir:(NSString*)srcPath to:(NSString*)desPath;
{
    NSError *error=nil;
    if([[NSFileManager defaultManager] moveItemAtPath:srcPath toPath:desPath error:&error]!=YES)// prePath 為原路徑、     cenPath 為目標(biāo)路徑
    {
        NSLog(@"移動(dòng)文件失敗");
        return NO;
    }
    else
    {
        NSLog(@"移動(dòng)文件成功");
        return YES;
    }
}

//創(chuàng)建文件
+ (BOOL)creatFile:(NSString*)filePath withData:(NSData*)data
{
    
   return  [[NSFileManager defaultManager] createFileAtPath:filePath contents:data attributes:nil];
}

//讀取文件
+(NSData*)readFile:(NSString *)filePath
{
    return [NSData dataWithContentsOfFile:filePath options:0 error:NULL];
}

//刪除文件
+(BOOL)deleteFile:(NSString *)filePath
{
    
    return [self deleteDir:filePath];
    
}

+ (NSString *)getFilePath:(NSString *)fileName
{
    NSString *dirPath = [[self getDocumentPath] stringByAppendingPathComponent:fileName];
    return dirPath;
}


+ (BOOL)writeDataToFile:(NSString*)fileName data:(NSData*)data
{
    NSString *filePath=[self getFilePath:fileName];
    return [self creatFile:filePath withData:data];
}

+ (NSData*)readDataFromFile:(NSString*)fileName
{
    NSString *filePath=[self getFilePath:fileName];
    return [self readFile:filePath];
}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容