美文网首页
利用runtime保存数据到本地文件,和保存到NS两种UserD

利用runtime保存数据到本地文件,和保存到NS两种UserD

作者: guoguojianshu | 来源:发表于2018-11-19 16:17 被阅读9次

    1.保存到本地的文件,放在pch文件中的

    #define USERFILE [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"hxj"]
    

    .h文件

    //
    //  DTXUserInfoManager.h
    //  dtx
    //
    //  Created by a on 2018/4/25.
    //  Copyright © 2018年 hnhf. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface DTXUserInfoManager : NSObject<NSCoding>
    +(instancetype)shareCurrentManager;
    //保存字典数据到本地
    -(void)saveUserInfoWithDict:(NSDictionary *)dict;
    //得到手机号
    -(NSString*)getPhone;
    //得到手机号运营商-认证状态 -1为未认证 1为认证通过 2为失败 3为认证中
    -(NSString*)getuserState;
    //用户银行卡管理表-是否默认
    -(NSString*)getbankCardState;
    
    /**
     用户信息认证表-认证状态
    
     @return return value description
     */
    -(NSString*)getcertificationState;
    //实名工作认证表-状态
    -(NSString*)getrealWorkState;
    
    /**
     支付分认证状态
     */
    -(NSString *)getzmScoreState;
    //用户联系表-状态
    -(NSString*)getrealRelativeState;
    //当前用户总额度
    -(NSString*)getallPrice;
    // 认证身份证号
    -(NSString*)getckCardNum;
    //认证真实姓名
    -(NSString *)getckName;
    //当前用户剩余额度
    -(NSString*)getrmPrice;
    //用户当前信息 ID 姓名 状态
    -(NSString *)gettoken;
    //获取交易密码的状态
    -(NSString *)getpasswordState;
    
    /**
     判断需要不要跳转贷超界面
    
     @return 1为不需要跳 -1需要跳转贷超界面
     */
    -(NSString *)getthreeAuthState;
    
    /**
     单据状态为1,2,8的数量
    
     @return 返回0或者1
     */
    -(NSString *)gethaveInHandLoanNum;
    
    /**
     订单状态 
    
     @return 1:未审核 2:已放款 3:审核中
     */
    -(NSString *)getbillSate;
    /**
     淘宝的状态
    
     @return 返回淘宝认证的状态值
     */
    -(NSString *)getTBState;
    //清除本地数据
    -(void)clearDomainData;
    
    
    
    
    
    /**
     账户总额度(double)
     */
    @property (nonatomic,strong)NSNumber *  allPrice;
    
    /**
     用户银行卡管理表-是否默认 -1为没有设置银行卡 1为设置银行卡
     */
    @property (nonatomic,copy)NSString * bankCardState;
    
    /**
     订单状态 1:未审核 2:已放款 3:审核中
     */
    @property (nonatomic,strong)NSNumber * billSate;
    
    /**
     黑名单状态
     */
    @property (nonatomic,copy)NSString * blackLState;
    
    /**
     身份认证状态-1为未认证 2为认证失败 3为认证中 1为认证成功
     */
    @property (nonatomic,copy)NSString * certificationState;
    
    /**
     身份证号码
     */
    @property (nonatomic,copy)NSString * ckCardNum;
    
    /**
     认证真实姓名
     */
    @property (nonatomic,copy)NSString * ckName;
    
    /**
     客户性质:1:新客2:次新3:老客
     */
    @property (nonatomic,strong)NSNumber * customer;
    
    /**
     单据的数量 返回0 或者1
     */
    @property (nonatomic,strong)NSNumber * haveInHandLoanNum;
    
    /**
     密码状态 -1为未设置 1为已设置
     */
    @property (nonatomic,copy)NSString * passwordState;
    
    /**
     手机号
     */
    @property (nonatomic,copy)NSString * phone;
    
    /**
     紧急联系人
     */
    @property (nonatomic,copy)NSString * realRelativeState;
    
    /**
     工作状态
     */
    @property (nonatomic,copy)NSString * realWorkState;
    
    /**
     余额
     */
    @property (nonatomic,copy)NSString * rmPrice;
    
    /**
     淘宝状态
     */
    @property (nonatomic,copy)NSString * tbState;
    
    /**
     -1为贷超 1不是贷超
     */
    @property (nonatomic,copy)NSString * threeAuthState;
    
    /**
     用户的token
     */
    @property (nonatomic,copy)NSString * token;
    
    /**
     运营商 -1为未认证 1为认证
     */
    @property (nonatomic,copy)NSString * userState;
    
    /**
     芝麻分
     */
    @property (nonatomic,copy)NSString * zmScoreState;
    
    //更新用户信息
    +(void)updateCurrentUser;
    //获取用户信息
    +(instancetype)getCurrentUser;
    +(BOOL)logout;
    @end
    

    .m文件

    //
    //  DTXUserInfoManager.m
    //  dtx
    //
    //  Created by a on 2018/4/25.
    //  Copyright © 2018年 hnhf. All rights reserved.
    //
    
    #import "DTXUserInfoManager.h"
    #import <objc/runtime.h>
    
    static DTXUserInfoManager * _user = nil;
    
    @implementation DTXUserInfoManager
    +(instancetype)shareCurrentManager{
        static DTXUserInfoManager *userManager = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            userManager = [[DTXUserInfoManager alloc]init];
        });
        return userManager;
    }
    -(void)saveUserInfoWithDict:(NSDictionary *)dict{
        NSArray *keyArr = [dict allKeys];
        for (NSString *keyString in keyArr) {
            if ([dict[keyString] isKindOfClass:[NSString class]]) {
                [[NSUserDefaults standardUserDefaults]setObject:dict[keyString] forKey:keyString];
            }else{
    //            [[NSUserDefaults standardUserDefaults]setInteger:dict[keyString] forKey:keyString];
                [[NSUserDefaults standardUserDefaults] setObject:dict[keyString] forKey:keyString];
            }
        }
        [[NSUserDefaults standardUserDefaults]synchronize];
    }
    -(NSString* )getPhone{
        
        return [[NSUserDefaults standardUserDefaults]objectForKey:@"phone"];
        
    }
    -(NSString*)getuserState{
        return [[NSUserDefaults standardUserDefaults]objectForKey:@"userState"];
    }
    -(NSString*)getbankCardState{
        return [[NSUserDefaults standardUserDefaults]objectForKey:@"bankCardState"];
    }
    -(NSString*)getcertificationState{
        return [[NSUserDefaults standardUserDefaults]objectForKey:@"certificationState"];
    }
    -(NSString*)getrealWorkState{
        return [[NSUserDefaults standardUserDefaults]objectForKey:@"realWorkState"];
    }
    -(NSString*)getrealRelativeState{
        return [[NSUserDefaults standardUserDefaults]objectForKey:@"realRelativeState"];
    }
    -(NSString*)getallPrice{
        return [[NSUserDefaults standardUserDefaults]objectForKey:@"allPrice"];
    }
    -(NSString*)getckCardNum{
        return [[NSUserDefaults standardUserDefaults]objectForKey:@"ckCardNum"];
    }
    -(NSString*)getckName{
        return [[NSUserDefaults standardUserDefaults]objectForKey:@"ckName"];
    }
    -(NSString*)getrmPrice{
        return [[NSUserDefaults standardUserDefaults]objectForKey:@"rmPrice"];
    }
    -(NSString*)gettoken{
        return [[NSUserDefaults standardUserDefaults]objectForKey:@"token"];
    }
    -(NSString *)getpasswordState{
        return [[NSUserDefaults standardUserDefaults]objectForKey:@"passwordState"];
    }
    /**
     判断需要不要跳转贷超界面
     
     @return 1为不需要跳 -1需要跳转贷超界面
     */
    -(NSString *)getthreeAuthState{
         return [[NSUserDefaults standardUserDefaults]objectForKey:@"threeAuthState"];
    }
    /**
     支付分认证状态
     
     */
    -(NSString *)getzmScoreState{
        return [[NSUserDefaults standardUserDefaults]objectForKey:@"zmScoreState"];
    
    }
    /**
     淘宝的状态
     
     @return 返回淘宝认证的状态值
     */
    -(NSString *)getTBState{
        return [[NSUserDefaults standardUserDefaults]objectForKey:@"tbState"];
    }
    /**
     单据状态为1,2,8的数量
     
     @return 返回0或者1
     */
    -(NSString *)gethaveInHandLoanNum{
        return [[NSUserDefaults standardUserDefaults]objectForKey:@"haveInHandLoanNum"];
    }
    /**
     订单状态 
     
     @return 1:未审核 2:已放款 3:审核中
     */
    -(NSString *)getbillSate{
        return [[NSUserDefaults standardUserDefaults]objectForKey:@"billSate"];
    }
    -(void)clearDomainData{
        NSString *bundleString =  [[NSBundle mainBundle] bundleIdentifier];
        [[NSUserDefaults standardUserDefaults]removePersistentDomainForName:bundleString];
        [[NSUserDefaults standardUserDefaults]synchronize];
    }
    
    
    
    -(void)encodeWithCoder:(NSCoder *)aCoder{
        unsigned int count = 0;
        Ivar * ivars =   class_copyIvarList([self class], &count);
        for (int i = 0; i<count; i++) {
            Ivar ivar = ivars[i];
            const char * name = ivar_getName(ivar);
            NSString * key = [NSString stringWithUTF8String:name];
            id value =  [self valueForKey:key];
    
            [aCoder encodeObject:value forKey:key];
        }
        free(ivars);
    }
    -(instancetype)initWithCoder:(NSCoder *)aDecoder{
        if (self = [super init]) {
            unsigned int count = 0;
            Ivar * ivars =    class_copyIvarList([self class], &count);
            for (int i = 0; i<count; i++) {
                Ivar ivar = ivars[i];
                const char * name = ivar_getName(ivar);
                NSString * key = [NSString stringWithUTF8String:name];
                id value = [aDecoder decodeObjectForKey:key];
                [self setValue:value forKey:key];
            }
            free(ivars);
        }
        return self;
    }
    
    +(void)updateCurrentUser{
        [NSKeyedArchiver archiveRootObject:_user toFile:USERFILE];
    }
    +(instancetype)getCurrentUser{
        if (_user == nil) {
            if ([[NSFileManager defaultManager]fileExistsAtPath:USERFILE]) {
                _user = [NSKeyedUnarchiver unarchiveObjectWithFile:USERFILE];
            }
            if (_user == nil) {
                _user = [[DTXUserInfoManager alloc]init];
                
            }
        }
        return _user;
    }
    +(BOOL)logout{
        NSFileManager * fileManager = [NSFileManager defaultManager];
        if ([fileManager fileExistsAtPath:USERFILE]) {
            return  [fileManager removeItemAtPath:USERFILE error:nil];
        }
        return NO;
    }
    @end
    

    相关文章

      网友评论

          本文标题:利用runtime保存数据到本地文件,和保存到NS两种UserD

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