iOS 储存用户信息设置封装 直接调用即可(部分是代码片段)
2017-09-25 09:40
603 查看
、、、、、、、、、、、、、、、、、、首先 定义 UserInfo
#import <Foundation/Foundation.h>
@interface UserInfo :
NSObject
//用户id
@property (nonatomic,
strong) NSString *userID;
//用户名
@property (nonatomic,
strong) NSString *userName;
//密码
@property (nonatomic,
strong) NSString *userPassword;
//昵称
@property (nonatomic,
strong) NSString *userNickName;
//用户头像
@property (nonatomic,
strong) NSString *HeadImg;
//注册日期
@property (nonatomic,
strong) NSString *createTime;
-(id)initWithDictionary:(NSDictionary *)dic;
@end
#import "UserInfo.h"
@implementation UserInfo
- (id)init {
if (self = [super
init]) {
self.userID =
@"";
self.userName =
@"";
self.userPassword =
@"";
self.userNickName =
@"";
self.HeadImg =
@"";
self.createTime =
@"";
}
return
self;
}
- (id)initWithDictionary:(NSDictionary *)dic {
if (self = [super
init]) {
self.userID = dic[@"userID"];
self.userName = dic[@"userName"];
self.userPassword = dic[@"userPassword"];
self.userNickName = dic[@"userNickName"];
self.HeadImg = dic[@"HeadImg"];
self.createTime = dic[@"createTime"];
}
return
self;
}
- (NSString *)description {
return [NSString
stringWithFormat:@"userID:%@,userName:%@,userPassword:%@,userNickName:%@,HeadImg:%@,createTime:%@",self.userID,self.userName,self.userPassword,self.userNickName,self.HeadImg,self.createTime];
}
-(void)encodeWithCoder:(NSCoder *)aCoder{
[self
yy_modelEncodeWithCoder:aCoder];
}
-(id)initWithCoder:(NSCoder *)aDecoder{
self = [super
init];
return [self
yy_modelInitWithCoder:aDecoder];
}
@end
、、、、、、、、、、、、、定义 UserManager
#import <Foundation/Foundation.h>
#import "UserInfo.h"
@interface UserManager :
NSObject
@property (nonatomic,
strong) UserInfo *userInfo;
//判断是否是登录状态
+(BOOL)isLogin;
//储存用户信息
+(void)saveUserObject:(UserInfo *)userinfo;
//获取用户基本信息
+(UserInfo *)getUserObject;
//退出登录,清除用户信息
+(void)logoOut;
@end
#import "UserManager.h"
@implementation UserManager
+ (BOOL)isLogin {
BOOL loginState;
NSUserDefaults *userDefault = [NSUserDefaults
standardUserDefaults];
NSData *data = [userDefault
objectForKey:@"1userObject"];
if (data.length >
0) {
loginState = YES;
}else{
loginState = NO;
}
return loginState;
}
+(void)saveUserObject:(UserInfo *)userinfo{
NSData *data = [NSKeyedArchiver
archivedDataWithRootObject:userinfo];
NSUserDefaults *userDefault = [NSUserDefaults
standardUserDefaults];
[userDefault setObject:data
forKey:[NSString
stringWithFormat:@"%@userObject",@"1"]];
}
+(UserInfo *)getUserObject{
NSUserDefaults *userDefault = [NSUserDefaults
standardUserDefaults];
NSData *data = [userDefault
objectForKey:@"1userObject"];
return [NSKeyedUnarchiver
unarchiveObjectWithData:data ];
}
+(void)logoOut{
[[NSUserDefaults
standardUserDefaults] removeObjectForKey:@"1userObject"];
}
@end
、、、、、是否登录判断
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (![UserManager
isLogin]) {
LoginViewController *vc = [[LoginViewController
alloc]
init];
UINavigationController *nav = [[UINavigationController
alloc]
initWithRootViewController:vc];
self.window.rootViewController= nav;
} else {
NSLog(@"%@",[UserManager
getUserObject]);
UserInfo *userInfo = [UserManager
getUserObject];
NSLog(@"%@",userInfo);
NSLog(@"zzzzz%@",userInfo.userName);
MainTabBarController *tabBar = [[MainTabBarController
alloc]
init];
self.window.rootViewController = tabBar;
}
return
YES;
}
、、、、、、、、、、、添加用户信息到本地设置
if ([str isEqualToString:@"101"]) {
[SVProgressHUD
showInfoWithStatus:@"登录成功"];
for (NSDictionary *dic
in [responseObject
objectForKey:@"data"]) {
UserInfo *userInfo = [[UserInfo
alloc] init];
userInfo.userID = [dic
objectForKey:@"id"];
userInfo.userName = [dic
objectForKey:@"username"];
userInfo.userPassword = [dic
objectForKey:@"password"];
userInfo.HeadImg = [dic
objectForKey:@"headimg"];
[UserManager
saveUserObject:userInfo];
NSLog(@"id%@",userInfo.userID);
NSLog(@"username%@",userInfo.userName);
NSLog(@"password%@",userInfo.userPassword);
NSLog(@"headimg%@",userInfo.HeadImg);
}
UIWindow *window = [[UIApplication
sharedApplication]
keyWindow];
MainTabBarController *VC = [MainTabBarController
alloc];
window.rootViewController = VC;
} else {
[SVProgressHUD
showErrorWithStatus:[responseObject objectForKey:@"msg"]];
}
#import <Foundation/Foundation.h>
@interface UserInfo :
NSObject
//用户id
@property (nonatomic,
strong) NSString *userID;
//用户名
@property (nonatomic,
strong) NSString *userName;
//密码
@property (nonatomic,
strong) NSString *userPassword;
//昵称
@property (nonatomic,
strong) NSString *userNickName;
//用户头像
@property (nonatomic,
strong) NSString *HeadImg;
//注册日期
@property (nonatomic,
strong) NSString *createTime;
-(id)initWithDictionary:(NSDictionary *)dic;
@end
#import "UserInfo.h"
@implementation UserInfo
- (id)init {
if (self = [super
init]) {
self.userID =
@"";
self.userName =
@"";
self.userPassword =
@"";
self.userNickName =
@"";
self.HeadImg =
@"";
self.createTime =
@"";
}
return
self;
}
- (id)initWithDictionary:(NSDictionary *)dic {
if (self = [super
init]) {
self.userID = dic[@"userID"];
self.userName = dic[@"userName"];
self.userPassword = dic[@"userPassword"];
self.userNickName = dic[@"userNickName"];
self.HeadImg = dic[@"HeadImg"];
self.createTime = dic[@"createTime"];
}
return
self;
}
- (NSString *)description {
return [NSString
stringWithFormat:@"userID:%@,userName:%@,userPassword:%@,userNickName:%@,HeadImg:%@,createTime:%@",self.userID,self.userName,self.userPassword,self.userNickName,self.HeadImg,self.createTime];
}
-(void)encodeWithCoder:(NSCoder *)aCoder{
[self
yy_modelEncodeWithCoder:aCoder];
}
-(id)initWithCoder:(NSCoder *)aDecoder{
self = [super
init];
return [self
yy_modelInitWithCoder:aDecoder];
}
@end
、、、、、、、、、、、、、定义 UserManager
#import <Foundation/Foundation.h>
#import "UserInfo.h"
@interface UserManager :
NSObject
@property (nonatomic,
strong) UserInfo *userInfo;
//判断是否是登录状态
+(BOOL)isLogin;
//储存用户信息
+(void)saveUserObject:(UserInfo *)userinfo;
//获取用户基本信息
+(UserInfo *)getUserObject;
//退出登录,清除用户信息
+(void)logoOut;
@end
#import "UserManager.h"
@implementation UserManager
+ (BOOL)isLogin {
BOOL loginState;
NSUserDefaults *userDefault = [NSUserDefaults
standardUserDefaults];
NSData *data = [userDefault
objectForKey:@"1userObject"];
if (data.length >
0) {
loginState = YES;
}else{
loginState = NO;
}
return loginState;
}
+(void)saveUserObject:(UserInfo *)userinfo{
NSData *data = [NSKeyedArchiver
archivedDataWithRootObject:userinfo];
NSUserDefaults *userDefault = [NSUserDefaults
standardUserDefaults];
[userDefault setObject:data
forKey:[NSString
stringWithFormat:@"%@userObject",@"1"]];
}
+(UserInfo *)getUserObject{
NSUserDefaults *userDefault = [NSUserDefaults
standardUserDefaults];
NSData *data = [userDefault
objectForKey:@"1userObject"];
return [NSKeyedUnarchiver
unarchiveObjectWithData:data ];
}
+(void)logoOut{
[[NSUserDefaults
standardUserDefaults] removeObjectForKey:@"1userObject"];
}
@end
、、、、、是否登录判断
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (![UserManager
isLogin]) {
LoginViewController *vc = [[LoginViewController
alloc]
init];
UINavigationController *nav = [[UINavigationController
alloc]
initWithRootViewController:vc];
self.window.rootViewController= nav;
} else {
NSLog(@"%@",[UserManager
getUserObject]);
UserInfo *userInfo = [UserManager
getUserObject];
NSLog(@"%@",userInfo);
NSLog(@"zzzzz%@",userInfo.userName);
MainTabBarController *tabBar = [[MainTabBarController
alloc]
init];
self.window.rootViewController = tabBar;
}
return
YES;
}
、、、、、、、、、、、添加用户信息到本地设置
if ([str isEqualToString:@"101"]) {
[SVProgressHUD
showInfoWithStatus:@"登录成功"];
for (NSDictionary *dic
in [responseObject
objectForKey:@"data"]) {
UserInfo *userInfo = [[UserInfo
alloc] init];
userInfo.userID = [dic
objectForKey:@"id"];
userInfo.userName = [dic
objectForKey:@"username"];
userInfo.userPassword = [dic
objectForKey:@"password"];
userInfo.HeadImg = [dic
objectForKey:@"headimg"];
[UserManager
saveUserObject:userInfo];
NSLog(@"id%@",userInfo.userID);
NSLog(@"username%@",userInfo.userName);
NSLog(@"password%@",userInfo.userPassword);
NSLog(@"headimg%@",userInfo.HeadImg);
}
UIWindow *window = [[UIApplication
sharedApplication]
keyWindow];
MainTabBarController *VC = [MainTabBarController
alloc];
window.rootViewController = VC;
} else {
[SVProgressHUD
showErrorWithStatus:[responseObject objectForKey:@"msg"]];
}
相关文章推荐
- 图片点击放大,再次点击返回原视图.完美封装,一个类一句代码即可调用.IOS完美实现
- 图片点击放大,再次点击返回原视图.完美封装,一个类一句代码即可调用.IOS完美实现
- iOS-封装FMDB框架:各用一句代码更新(添加&修改),查询,删除用户信息
- ios 对于AFNetworking3.0之前版本的Get、Post请求的封装(直接调用即可)
- 图片点击放大,再次点击返回原视图.完美封装,一个类一句代码即可调用.IOS完美实现
- IOS中UserDefaults的用法(储存和调用设置信息)
- 图片点击放大,再次点击返回原视图.完美封装,一个类一句代码即可调用.IOS完美实现
- 图片点击放大,再次点击返回原视图.完美封装,一个类一句代码即可调用.IOS完美实现
- iOS开发swift如何调用系统相册和相机获取图片设置用户头像
- ios中javascript直接调用oc代码而非通过改变url回调方式(转)
- iOS开发,javascript直接调用oc代码而非通过改变url回调方式
- ios开发,javascript直接调用oc代码而非通过改变url回调方式
- .NET 的钩子组件……(封装起来使得你不用写代码即可完成钩子的设置)(转)
- iOS学习笔记77-ios开发,javascript直接调用oc代码而非通过改变url回调方式
- ios设置导航栏透明方法:使用只有一个像素点的透明图片,通过以下代码即可
- (安卓APP)关于使用偏好设置储存数据,首次登录应用到新手指导页,再次登录直接到主页面简单的相关实现代码
- 车辆信息管理系统(部分模块待改进),直接运行即可,绝对原创
- iOS开发-给TableView或者CollectionView的cell添加简单动画,很方便,直接粘代码即可