您的位置:首页 > 移动开发 > IOS开发

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"]];

        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐