写在前面
开发遇坑1———登录注册用户
凡事开发一个产品型app总是免不了需要使用用户登录注册这一套,在网上也没有看到特别清晰的逻辑流程,在这里我就拿我的项目给大家介绍一下吧
1.登录 登录几乎是主导页面,里面存在两个跳转功能-注册、忘记密码页面 页面跳转推荐大家使用

-(void)forgetPsdAction
{
ForgetPsdCtrl *rootCtrl = [[ForgetPsdCtrl alloc]init];
//包装一个Navigation
UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:rootCtrl];
//动画进行跳转
[UIView transitionWithView:self.view.window
duration:.4 options:UIViewAnimationOptionTransitionNone animations:^{
self.view.window.rootViewController = navi;
} completion:NULL];
[UIApplication sharedApplication].keyWindow.rootViewController = rootCtrl;
}
这个方法啊,页面保证释放(页面循环跳转,若有页面不进行释放的话则会导致循环引用)
2.就忘记密码和注册页面来看也就是相互之间可以跳转到登录页面去。
3.一旦登录成功,这时候后台服务器回返回一个用户的所有信息的json或者其他类型的数据过来。
4.拿json数据为例子,你需要在本地建立一个model对象来进行保存你接收的这些数据,当你拿到这些数据的时候,你需要进行数据持久化。
说到数据持久化,我在这里需要用NSUserDefaults来用存储
NSUserDefaults直接存储的只能够存储最基本的数据,比如NSArray/NSString等基本的对象,如果需要存储自己创建的对象的话需要将对象转换为NSData对象进行存储
这是自定义对象部分
#import <JSONModel/JSONModel.h>
@interface UserDataModel : JSONModel<NSCoding>
@property(nonatomic,copy)NSString <Optional>* classInfoId; //班级id
@property(nonatomic,copy)NSString <Optional>* className; //班级名称
@property(nonatomic,assign)NSInteger dataComplete; //完成程度
@property(nonatomic,copy)NSString <Optional>*gradeName; //年级
@property(nonatomic,copy)NSString <Optional>*isExist; //是否填报过
@property(nonatomic,copy)NSString <Optional>*mail; //邮箱
@property(nonatomic,copy)NSString <Optional>*parentsName; //家长的姓名
@property(nonatomic,copy)NSString <Optional>*parentsPhone; //家长的电话
@property(nonatomic,copy)NSString *phoneNum; //手机号
@property(nonatomic,copy)NSString *roleName; //身份名字
@property(nonatomic,copy)NSString <Optional>*schoolCity; //学校城市
@property(nonatomic,copy)NSString <Optional>*schoolArea; //学校地区
@property(nonatomic,copy)NSString <Optional>*schoolCityId ; //学校城市ID
@property(nonatomic,copy)NSString <Optional>*schoolName; //学校名称
@property(nonatomic,copy)NSString <Optional>*schoolProvince; //学校的省份
在.m里面添加这些代码
//反归档方法
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.classInfoId forKey:@"classInfoId"];
[aCoder encodeObject:self.className forKey:@"className"];
[aCoder encodeInteger:self.dataComplete forKey:@"dataComplete"];
[aCoder encodeObject:self.gradeName forKey:@"gradeName"];
[aCoder encodeObject:self.isExist forKey:@"isExist"];
[aCoder encodeObject:self.mail forKey:@"mail"];
[aCoder encodeObject:self.parentsName forKey:@"parentsName"];
[aCoder encodeObject:self.parentsPhone forKey:@"parentsPhone"];
[aCoder encodeObject:self.phoneNum forKey:@"phoneNum"];
[aCoder encodeObject:self.roleName forKey:@"roleName"];
[aCoder encodeObject:self.schoolCity forKey:@"schoolCity"];
[aCoder encodeObject:self.schoolArea forKey:@"schoolArea"];
[aCoder encodeObject:self.schoolProvince forKey:@"schoolProvince"];
[aCoder encodeObject:self.schoolCityId forKey:@"schoolCityId"];
[aCoder encodeObject:self.schoolName forKey:@"schoolName"];
[aCoder encodeObject:self.teacherName forKey:@"teacherName"];
[aCoder encodeObject:self.teacherPhone forKey:@"teacherPhone"];
[aCoder encodeObject:self.token forKey:@"token"];
[aCoder encodeObject:self.userId forKey:@"userId"];
[aCoder encodeObject:self.userName forKey:@"userName"];
[aCoder encodeObject:self.userGender forKey:@"userGender"];
[aCoder encodeObject:self.version forKey:@"version"];
[aCoder encodeObject:self.schoolInfoId forKey:@"schoolInfoId"];
[aCoder encodeObject:self.collegeName forKey:@"collegeName"];
[aCoder encodeObject:self.majorName forKey:@"majorName"];
[aCoder encodeObject:self.imgPath forKey:@"imgPath"];
}
//以及归档方法
-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
self.classInfoId = [aDecoder decodeObjectForKey:@"classInfoId"];
self.className = [aDecoder decodeObjectForKey:@"className"];
self.dataComplete = [aDecoder decodeIntegerForKey:@"dataComplete"];
self.gradeName = [aDecoder decodeObjectForKey:@"gradeName"];
self.isExist = [aDecoder decodeObjectForKey:@"isExist"];
self.mail = [aDecoder decodeObjectForKey:@"mail"];
self.parentsName = [aDecoder decodeObjectForKey:@"parentsName"];
self.parentsPhone = [aDecoder decodeObjectForKey:@"parentsPhone"];
self.roleName = [aDecoder decodeObjectForKey:@"roleName"];
self.phoneNum = [aDecoder decodeObjectForKey:@"phoneNum"];
self.schoolCity = [aDecoder decodeObjectForKey:@"schoolCity"];
self.schoolArea = [aDecoder decodeObjectForKey:@"schoolArea"];
self.schoolCityId = [aDecoder decodeObjectForKey:@"schoolCityId"];
self.schoolName = [aDecoder decodeObjectForKey:@"schoolName"];
self.schoolProvince =[aDecoder decodeObjectForKey:@"schoolProvince"];
self.teacherName = [aDecoder decodeObjectForKey:@"teacherName"];
self.teacherPhone = [aDecoder decodeObjectForKey:@"teacherPhone"];
self.token = [aDecoder decodeObjectForKey:@"token"];
self.userGender = [aDecoder decodeObjectForKey:@"userGender"];
self.userId = [aDecoder decodeObjectForKey:@"userId"];
self.userName = [aDecoder decodeObjectForKey:@"userName"];
self.version = [aDecoder decodeObjectForKey:@"version"];
self.schoolInfoId = [aDecoder decodeObjectForKey:@"schoolInfoId"];
self.collegeName = [aDecoder decodeObjectForKey:@"collegeName"];
self.majorName = [aDecoder decodeObjectForKey:@"majorName"];
self.imgPath = [aDecoder decodeObjectForKey:@"imgPath"];
}
return self;
}


虽然很繁琐,但是这是必须要的
然后你具体写文件的时候就需要写入

”’ //写文件不能直接写入一个对象,需要将其转换为NSData对象后存储
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:userDataModel];
[userDefaults setObject:data forKey:userdataMModel];
//别忘了用这句代码给你的写入加入同步机制
[userDefaults synchronize];
将自定义的对象进行归档
反归档也很容易,你只需要
//读取user对象
NSData *fanData = [userDefaults objectForKey:userdataMModel];
UserDataModel *userdatamodel = [NSKeyedUnarchiver unarchiveObjectWithData:fanData];

读取userdefult里面的数据用NSData接收即可
怎么样,小伙伴们学会了吗~以后会逐步更新博客,来进行交流的,有错误的地方欢迎大家指正哦!~