沙盒

作者: MI移动 | 来源:发表于2017-07-20 11:22 被阅读0次

    沙盒

    #import "RootViewController.h"
    
    @interface RootViewController ()
    
    @end
    
    @implementation RootViewController
    
    // 数据持久化
    
    // 一些不可再生的数据可以进行数据持久化,说白了就是放进APP的文件中,而可再生的数据,是放在内存中的;可以被适当
    
    // 沙盒(SandBox),他的本质是一个文件夹,叫沙盒的原因是因为它符合一种安全机制
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor purpleColor];
       
        // 沙盒路径 : 每次安装应用程序,都会产生一个沙盒,沙盒的路径非常深,并且经过了随机加密
       
        // 沙盒的使用,导致APP都是自身管理自身,在没有权限或者许可的情况下,两个不同的APP不可以互相访问
       
        // 1.
    //    NSString *userName = NSUserName();
    //    NSString *sandBoxPath = NSHomeDirectoryForUser(userName);
    //    
    //    NSLog(@"%@",sandBoxPath);
       
        // 2.
        NSString *sandBoxPath = NSHomeDirectory();
        NSLog(@"%@",sandBoxPath);
        // 沙盒文件
     /*
        // 沙盒中有三个文件夹
           // 1. Document    存储用户数据,需要备份的信息 (一般是那些被数据持久化的数据)
           // 会被iTunes同步
        NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
        NSLog(@"%@",documentPath);
       
            // 2. Library/Caches 存储缓存文件 (一般是一些图片和视频的缓存)
            // 不会被iTunes不会被同步
        NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
        NSLog(@"%@",cachesPath);
       
            // 3. Library/Preferences 存储应用程序的偏好设置文件
            // 会被iTunes同步
        NSString *preferencesPath = NSSearchPathForDirectoriesInDomains(NSPreferencePanesDirectory, NSUserDomainMask, NO)[0];
           // 如果把参数改成NO 沙盒路径前面会省略用波浪线代替
        NSLog(@"%@",preferencesPath);
       
           // 4. tmp 临时文件夹,放临时文件,退出APP的时候,里面文件被清除
           // 不会被iTunes同步
        NSString *tempPath = NSTemporaryDirectory();
        NSLog(@"%@",tempPath);
       
            // 5. iOS8之前,沙盒还有一个.app的文件夹
        NSString *appPath = [[NSBundle mainBundle] bundlePath];
        NSLog(@"%@",appPath);
       
      */
    

    简单对象写入文件

        /*
        NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
       
        NSString *filePath  = [NSString stringWithFormat:@"%@/张三的战书.txt",documentPath];
        NSString *loveContent = @"iLoveU";
        // 将内容写入文件
        [loveContent writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    //    NSLog(@"%@",filePath);
        NSString *filePath2 =  [documentPath stringByAppendingString:@"/myVideo.avi"];
        NSString *aviContent = @"爱情动作片";
        [aviContent writeToFile:filePath2 atomically:YES encoding:NSUTF8StringEncoding error:nil];
       NSLog(@"%@",filePath2);
    
        // 新建一个文件
       
        // 写入一个简单对象 NSArray NSDictionAry
        NSArray *arr = [NSArray arrayWithObjects:@"da",@"fag",@"afgthfd", nil];
        [arr writeToFile:filePath atomically:YES];
        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"a",@"2",@"b",@"3",@"c", nil];
        [dict writeToFile:filePath atomically:YES];
       
        NSLog(@"%@",filePath);
       
        // 读取文件中的内容
        */
       
    

    NSFileManager 文件管理者

     //1. 新建文件夹 2. 可以创建 移动 复制 删除文件   3.判断文件是否存在
    
     NSFileManager *manager = [NSFileManager defaultManager];
    
     // 1. 新建文件夹
     [manager createDirectoryAtPath:[sandBoxPath stringByAppendingString:@"/我的"]withIntermediateDirectories:YES attributes:nil error:nil];
     NSLog(@"%@",sandBoxPath);
    
     // 删除
    // [manager removeItemAtPath:[sandBoxPath stringByAppendingString:@"/我的"] error:nil];
    
     // 移动
    //  [manager moveItemAtPath: toPath: error:;]
    
     // 复制
    //   [manager copyItemAtPath: toPath: error:];
    
     // 判断是否存在
    //    - (BOOL)fileExistsAtPath:(NSString *)path;
    //    - (BOOL)fileExistsAtPath:(NSString *)path isDirectory:(BOOL *)isDirectory;
    //    - (BOOL)isReadableFileAtPath:(NSString *)path;
    //    - (BOOL)isWritableFileAtPath:(NSString *)path;
    //    - (BOOL)isExecutableFileAtPath:(NSString *)path;
    //    - (BOOL)isDeletableFileAtPath:(NSString *)path;
    
    }
    

    复杂对象写入沙盒

    #import <Foundation/Foundation.h>
    
    // 1. 遵循协议
    @interface Person : NSObject<NSCoding>
    
    @property (nonatomic,strong)NSString *name;
    @property(nonatomic,strong)NSString *gender;
    
    
    @end
    
    
    #import "Person.h"
    
    @implementation Person
    
    // 实现NSCoding的编码和反编码方法
    
    // 编码方式
    - (void)encodeWithCoder:(NSCoder *)aCoder{
        // 把model类里面的属性进行编码
        [aCoder encodeObject:self.name  forKey:@"p_name"];
        [aCoder encodeObject:self.gender forKey:@"p_gender"];
    }
    
    // 反编码方法
    - (id)initWithCoder:(NSCoder *)aDecoder{
        if (self = [super init]) {
            // 反编码的时候的时候要找返回值接受
       self.name = [aDecoder decodeObjectForKey:@"p_name"];
        self.gender =  [aDecoder decodeObjectForKey:@"p_gender"];
        };
        return self;
    }
    
    @end
    
    
    
    #import "RootViewController.h"
    #import "Person.h"
    
    @interface RootViewController ()
    
    @end
    
    @implementation RootViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
       
    // document路径
        NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,  YES)[0];
        NSString *filePath = [documentPath stringByAppendingString:@"/小明.txt"];
    //    NSString *tmpStr = @"我爱台妹";
    //    
    //    [tmpStr writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    //    
        Person *p1 = [[Person alloc]init];
       
        // 由于p1是复杂对象,没有办法直接写入文件
       
        // 简单对象可以直接写入对象的原因是因为NSString,NSArray,NSdictionary,NSNumber  这些对象他们遵循了NSCoding 协议
       
        p1.name  = @"张三";
        p1.gender = @"男";
       
        NSMutableData *data1 = [NSMutableData data];
       
        // 归档工具
        NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data1];
       
        [archiver encodeObject:p1 forKey:@"person"];
        [archiver finishEncoding];
       
        [data1 writeToFile:filePath atomically:YES];
        NSLog(@"%@",filePath);
       
        // 读取 (反归档)
        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data1];
        Person *p2 =  [unarchiver decodeObjectForKey:@"person"];
        NSLog(@"name:%@ gender:%@",p2.name,p2.gender);
        [unarchiver finishDecoding];
     
    }
    

    相关文章

      网友评论

          本文标题:沙盒

          本文链接:https://www.haomeiwen.com/subject/agydkxtx.html