博客> 用户的登录注册
用户的登录注册
2017-09-24 05:17 评论:0 阅读:817 青春追逐
登录注册

我们将信息保存起来 下次打开应用程序 直接从沙盒中读取比对判断 十分方便

这里封装了一个用户模型 和 一个工具类 大家可以拿来直接使用

代码如下:注解很详细

[objc] view plain copy print?

import

warning 这里必须遵守协议 不然不知道对象中的哪些属性写进沙盒

@interface QHAccount : NSObject

//返回字段 字段类型 字段说明

/*string 登录状态 返回值1 表示登录成功 返回值0表示登录失败/
@property (nonatomic, copy) NSString *status;

/*string 记录登录状态信息:登录成功/登录失败/
@property (nonatomic, copy) NSString *data;

/*string userid 用户的id 编号唯一/
@property (nonatomic, copy) NSString *userid;

/*string mobile电话号码/
@property(nonatomic, copy)NSString *mobile;

/*string nickname昵称 用于保存用户的昵称/
@property (nonatomic, copy) NSString *nickname;

/*string 登录成功后返回token值/
@property (nonatomic, copy)NSString *
token;

  • (instancetype)accountWithDict:(NSDictionary *)dict;

@end

[objc] view plain copy print?

import "QHAccount.h"

@implementation QHAccount

  • (instancetype)accountWithDict:(NSDictionary )dict
    {
    QHAccount
    account = [[self alloc]init];

    account.status = dict[@"status"];
    account.data = dict[@"data"];
    account.userid = dict[@"userid"];
    account.mobile = dict[@"mobile"];
    account.nickname = dict[@"nickname"];
    account.token = dict[@"token"];

    return account;
    }

/**

  • 当一个对象要归档到沙盒中的时候 就会调用这个方法 目的:在这个方法中说明这个对象的哪些属性要存进沙盒

    */

  • (void)encodeWithCoder:(NSCoder *)aCoder
    {
    [aCoder encodeObject:self.status forKey:@"status"];
    [aCoder encodeObject:self.data forKey:@"data"];
    [aCoder encodeObject:self.userid forKey:@"userid"];
    [aCoder encodeObject:self.mobile forKey:@"mobile"];
    [aCoder encodeObject:self.nickname forKey:@"nickname"];
    [aCoder encodeObject:self.token forKey:@"token"];
    }

/**

  • 当从沙盒中解档一个对象(从沙盒中加载一个对象)就要调用该方法 目的:在这个方法中说明沙盒中的属性该怎么解析(需要取出哪些属性) */
  • (id)initWithCoder:(NSCoder *)aDecoder
    {
    if (self = [super init]) {
    self.status = [aDecoder decodeObjectForKey:@"status"];
    self.data = [aDecoder decodeObjectForKey:@"data"];
    self.userid = [aDecoder decodeObjectForKey:@"userid"];
    self.mobile = [aDecoder decodeObjectForKey:@"mobile"];
    self.nickname = [aDecoder decodeObjectForKey:@"nickname"];
    self.token = [aDecoder decodeObjectForKey:@"token"];
    }
    return self;
    }
    @end

[objc] view plain copy print?

import

import "QHAccount.h"

@interface QHAccountTool : NSObject

/**

  • 存储用户的账号信息
  • @param account <#account description#> */
    • (void)saveAccount:(QHAccount *)account;

/**

  • 返回账号信息
  • @return <#return value description#> */
    • (QHAccount *)account;

@end

//处理账号所有的有关信息 存储账号 验证账号

define QHAccountPath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"account.archive"]

import "QHAccountTool.h"

@implementation QHAccountTool

/**

  • 存储用户的账号信息
  • @param account <#account description#> */
    • (void)saveAccount:(QHAccount )account { //放进document 可以进行备份 //将返回的数据存进沙盒 // NSString doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];

warning 这里的NSString 方法调用出错用的是stringByAppending 路径拼接错误

//    NSString *path = [doc stringByAppendingPathComponent:@"account.archive"];
//存进沙盒
//[responseObject writeToFile:path atomically:YES];

//自定义对象的存储必须用NSKeyedArchiver 不在有什么write to file

[NSKeyedArchiver archiveRootObject:account toFile:QHAccountPath];

}

/**

  • 返回账号信息
  • @return <#return value description#> */
    • (QHAccount )account { //加载数据模型 QHAccount account = [NSKeyedUnarchiver unarchiveObjectWithFile:QHAccountPath]; return account; } @end
收藏
1
sina weixin mail 回到顶部