国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

OS的沙盒機(jī)制--基礎(chǔ)知識

2019-11-14 20:08:41
字體:
供稿:網(wǎng)友

 /*

iOS的沙盒機(jī)制,應(yīng)用只能訪問自己應(yīng)用目錄下的文件。

iOS不像android,沒有SD卡概念,不能直接訪問圖像、視頻等內(nèi)容。
 iOS應(yīng)用產(chǎn)生的內(nèi)容,如圖像、文件、緩存內(nèi)容等都必須存儲在自己的沙盒內(nèi)。
 默認(rèn)情況下,每個沙盒含有3個文件夾:Documents, Library 和 tmp。Library包含Caches、PReferences目錄。
 上面的完整路徑為:用戶->資源庫->application Support->iphone Simulator->7.1->Aplications
 
 Documents:蘋果建議將程序創(chuàng)建產(chǎn)生的文件以及應(yīng)用瀏覽產(chǎn)生的文件數(shù)據(jù)保存在該目錄下,iTunes備份和恢復(fù)的時候會包括此目錄
 Library:存儲程序的默認(rèn)設(shè)置或其它狀態(tài)信息;
 Library/Caches:存放緩存文件,保存應(yīng)用的持久化數(shù)據(jù),用于應(yīng)用升級或者應(yīng)用關(guān)閉后的數(shù)據(jù)保存,不會被itunes同步,所以為了減少同步的時間,可以考慮將一些比較大的文件而又不需要備份的文件放到這個目錄下。
 tmp:提供一個即時創(chuàng)建臨時文件的地方,但不需要持久化,在應(yīng)用關(guān)閉后,該目錄下的數(shù)據(jù)將刪除,也可能系統(tǒng)在程序不運(yùn)行的時候清除
 */
#import "NSFileManagerViewController.h"

@interface NSFileManagerViewController ()

@end

@implementation NSFileManagerViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //獲取沙盒目錄
    NSString *dirHome = NSHomeDirectory();
    NSLog(@"APP_home:%@",dirHome);
    
    [self dirDoc];
    [self dirLib];
    [self dirCache];
    [self dirTmp];
    [self createDir];
    [self createFile];
    [self redFile];
    [self  fileAttriutes];
    [self deleteFile];
}

//獲取Document目錄路徑
- (NSString *)dirDoc {
    //[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [path objectAtIndex:0];
    NSLog(@"app_home_doc:%@",documentsDirectory);
    return documentsDirectory;
}

-(void)dirLib{
    //[NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *libraryDirectory = [path objectAtIndex:0];
    NSLog(@"app_home_lib:%@",libraryDirectory);
}

- (void)dirCache{
    NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachePath = [cacPath  objectAtIndex:0];
    NSLog(@"app_home_lib:%@",cachePath);
}

- (void)dirTmp{
    //[NSHomeDirectory() stringByAppendingPathComponent:@"tmp"];
    NSString *tmpDirectory = NSTemporaryDirectory();
    NSLog(@"app_home_tmp: %@",tmpDirectory);
}

// 穿件文件夾
-(void)createDir{
    NSString *documentsPAth = [self dirDoc];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *testDirectory = [documentsPAth stringByAppendingString:@"test"];
    //創(chuàng)建目錄
    BOOL res = [fileManager createDirectoryAtPath:testDirectory
                      withIntermediateDirectories:YES
                                       attributes:nil
                                            error:nil];
    if(res)
    {
        NSLog(@"文件夾創(chuàng)建成功");
    }
    else
    {
        NSLog(@"文件夾創(chuàng)建失敗");
    }
}

//創(chuàng)建文件
- (void)createFile {
    NSString *documentPath = [self dirDoc];
    NSString *testDirectory = [documentPath stringByAppendingPathComponent:@"test"];
    NSFileManager *filemanager = [NSFileManager defaultManager];
    NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
    BOOL res = [filemanager createFileAtPath:testPath contents:nil attributes:nil];
    if (res) {
        NSLog(@"文件創(chuàng)建成功:%@",testPath);
    }else{
        NSLog(@"文件創(chuàng)建失敗");
    }
}
- (void)writeFile{
    NSString *documentsPath = [self dirDoc];
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
    NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
    NSString *content=@"測試寫入內(nèi)容!";
    BOOL res=[content writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    if (res) {
        NSLog(@"文件寫入成功");
    }else
        NSLog(@"文件寫入失敗");
}

//讀文件
- (void)redFile
{
    NSString *documentsPath =[self dirDoc];
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
    NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
    //    NSData *data = [NSData dataWithContentsOfFile:testPath];
    //    NSLog(@"文件讀取成功: %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    NSString *content=[NSString stringWithContentsOfFile:testPath encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"文件讀取成功: %@",content);
}

//文件屬性
-(void)fileAttriutes{
    NSString *documentsPath =[self dirDoc];
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
    NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:testPath error:nil];
    NSArray *keys;
    id key, value;
    keys = [fileAttributes allKeys];
    int count = [keys count];
    for (int i = 0; i < count; i++)
    {
        key = [keys objectAtIndex: i];
        value = [fileAttributes objectForKey: key];
        NSLog (@"Key: %@ for value: %@", key, value);
    }
}
//刪除文件
-(void)deleteFile{
    NSString *documentsPath =[self dirDoc];
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
    BOOL res=[fileManager removeItemAtPath:testPath error:nil];
    if (res) {
        NSLog(@"文件刪除成功");
    }else
        NSLog(@"文件刪除失敗");
    NSLog(@"文件是否存在: %@",[fileManager isExecutableFileAtPath:testPath]?@"YES":@"NO");
}



發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 射阳县| 乌鲁木齐县| 乌兰县| 穆棱市| 博白县| 交城县| 宜兰县| 西畴县| 江阴市| 唐山市| 西乡县| 滨州市| 锡林郭勒盟| 景洪市| 海淀区| 平和县| 汤阴县| 祥云县| 墨竹工卡县| 昂仁县| 中西区| 清原| 油尖旺区| 石嘴山市| 凤翔县| 许昌市| 永修县| 全椒县| 寿阳县| 出国| 当雄县| 吉安市| 六枝特区| 文登市| 阿巴嘎旗| 荣成市| 大厂| 大理市| 封开县| 兰考县| 永和县|