zoukankan      html  css  js  c++  java
  • iOS开发--沙盒

    IOS中的沙盒机制(SandBox)是一种安全体系,它规定了应用程序只能在为该应用创建的文件夹内读取文件,不可以访问其他地方的内容。所有的非代码文件都保存在这个地方,比如图片、声音、属性列表和文本文件等。

    1.每个应用程序都在自己的沙盒内

    2.不能随意跨越自己的沙盒去访问别的应用程序沙盒的内容

    3.应用程序向外请求或接收数据都需要经过权限认证

    查看模拟器的沙盒文件夹在Mac 电脑上隐藏的。

    1、找到资料库,首先切换到 Finder 程序,点击顶部“前往”菜单,按住option键,即可显示“资料库”,点击打开。

    2、打开沙盒.

    Xcode5.1 资源库—Application Support—iPhone Simulator—7.1—Application—按修改日期排序,最新一个—Documents。

    Xcode 6 在Finder上点->前往->前往文件夹,输入/Users/ocq/Library/Developer/CoreSimulator/Devices/3BE436D4-F82B-42A1-A2AE-5244799F9A0F/data/Containers/Data/Application/05ADDB2D-E1CE-465C-AFAE-9E149837F4C7/Documents  前往即可打开目录。

    还有一种更方便的方式,给代码打个断点,输入:po NSHomeDirectory() 就会输入路径

    将数据存入沙盒有多种方式,我使用的是:

    1、存写字符串及字典等:

            NSString *doc=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
            NSString *path=[doc stringByAppendingPathComponent:@"account.plish"];
            //responseObject是返回的内容
            [responseObject writeToFile:path atomically:YES];

    2、存储自定义的信息,使用NSKeyedArchiver:

           
            NSString *doc=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
            NSString *path=[doc stringByAppendingPathComponent:@"account.archive"];
            //esponseObject是返回的内容转为模型
            HWAccount *account=[HWAccount accountWithDict:responseObject];
            [NSKeyedArchiver archiveRootObject:account toFile:path];

    实际使用,存储登陆用户的信息:

    1、定义一个用户模型

    NJAccountModel.h

    //
    //  NJAccountModel.h
    //  NJWisdomCard
    //
    //  Created by apple on 15/8/31.
    //  Copyright (c) 2015年 Weconex. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface NJAccountModel : NSObject
    /**    唯一注册号*/
    @property (nonatomic, copy) NSString *custuuid;
    
    /**    用户姓名*/
    @property (nonatomic, copy) NSString *custname;
    
    /**    手机号*/
    @property (nonatomic, copy) NSString *mobile;
    
    /**    证件号*/
    @property (nonatomic, copy) NSString *socialcode;
    
    @end

     NJAccountModel.m

    //
    //  NJAccountModel.m
    //  NJWisdomCard
    //
    //  Created by apple on 15/8/31.
    //  Copyright (c) 2015年 Weconex. All rights reserved.
    //
    
    #import "NJAccountModel.h"
    
    @implementation NJAccountModel
    /**
     *  当一个对象要归档进沙盒中时,就会调用这个方法
     *  目的:在这个方法中说明这个对象的哪些属性要存进沙盒
     */
    - (void)encodeWithCoder:(NSCoder *)encoder
    {
        [encoder encodeObject:self.custname forKey:@"custname"];
        [encoder encodeObject:self.custuuid forKey:@"custuuid"];
        [encoder encodeObject:self.socialcode forKey:@"socialcode"];
        [encoder encodeObject:self.mobile forKey:@"mobile"];
    }
    
    /**
     *  当从沙盒中解档一个对象时(从沙盒中加载一个对象时),就会调用这个方法
     *  目的:在这个方法中说明沙盒中的属性该怎么解析(需要取出哪些属性)
     */
    - (id)initWithCoder:(NSCoder *)decoder
    {
        if (self = [super init]) {
            self.custname = [decoder decodeObjectForKey:@"custname"];
            self.custuuid = [decoder decodeObjectForKey:@"custuuid"];
            self.socialcode = [decoder decodeObjectForKey:@"socialcode"];
            self.mobile = [decoder decodeObjectForKey:@"mobile"];
        }
        return self;
    }
    @end

    2.读取沙盒里的用户信息还是挺频繁的,封装成一个工具类,提供存入沙盒和读取沙盒的方法。

    NJAccountTool.h

    //
    //  NJAccountTool.h
    //  NJWisdomCard
    //
    //  Created by apple on 15/8/31.
    //  Copyright (c) 2015年 Weconex. All rights reserved.
    //  处理账号相关的所有操作:存储账号、取出账号
    
    #import <Foundation/Foundation.h>
    #import "NJAccountModel.h"
    
    @interface NJAccountTool : NSObject
    
    /**
     *  存储账号信息
     *
     *  @param account 账号模型
     */
    + (void)saveAccount:(NJAccountModel *)accountModel;
    
    /**
     *  返回账号信息
     *
     *  @return 账号模型(如果账号过期,返回nil)
     */
    + (NJAccountModel *)accountModel;
    @end

    NJAccountTool.m

    //
    //  NJAccountTool.m
    //  NJWisdomCard
    //
    //  Created by apple on 15/8/31.
    //  Copyright (c) 2015年 Weconex. All rights reserved.
    //
    // 账号的存储路径
    #define NJAccountPath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"account.archive"]
    
    #import "NJAccountTool.h"
    
    @implementation NJAccountTool
    
    /**
     *  存储账号信息
     *
     *  @param accountModel 账号模型
     */
    + (void)saveAccount:(NJAccountModel *)accountModel
    {
        // 自定义对象的存储用NSKeyedArchiver
        [NSKeyedArchiver archiveRootObject:accountModel toFile:NJAccountPath];
    }
    
    /**
     *  返回账号信息
     *
     *  @return 账号模型
     */
    + (NJAccountModel *)accountModel
    {
        // 加载模型
        NJAccountModel *accountModel = [NSKeyedUnarchiver unarchiveObjectWithFile:NJAccountPath];
        
        return accountModel;
    }
    @end

    3.接下来,在其它地方调用就很容易了。

    引用头文件:

    #import "NJAccountTool.h"

    存在沙盒:

         NJAccountModel *accountModel=[NJAccountModel objectWithKeyValues:responseObject[@"code"]];
          // 存储账号信息到沙盒
          [NJAccountTool saveAccount:accountModel];

    读取沙盒:

    NJAccountModel *accountModel=[NJAccountTool accountModel];
  • 相关阅读:
    题解——洛谷P3812【模板】线性基
    题解——洛谷P2781 传教(线段树)
    题解——洛谷P1962 斐波那契数列(矩阵乘法)
    题解——洛谷P3390 【模板】矩阵快速幂(矩阵乘法)
    题解——牛客网OI赛制测试赛2
    题解——code[vs] 1506 传话(传递闭包)
    题解——Codeforces Round #508 (Div. 2) T3 (贪心)
    题解——Codeforces Round #508 (Div. 2) T2 (构造)
    题解——Codeforces Round #508 (Div. 2) T1 (模拟)
    题解——Codeforces Round #507 (based on Olympiad of Metropolises) T2(模拟)
  • 原文地址:https://www.cnblogs.com/jys509/p/4472825.html
Copyright © 2011-2022 走看看