iOS 储存用户信息设置封装 直接调用即可(部分是代码片段)

来源:互联网 发布:top100高频算法 编辑:程序博客网 时间:2024/06/03 13:47

、、、、、、、、、、、、、、、、、、首先 定义   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 = [superinit]) {

        self.userID       =@"";

        self.userName     =@"";

        self.userPassword =@"";

        self.userNickName =@"";

        self.HeadImg      =@"";

        self.createTime   =@"";

    }

    return self;

}


- (id)initWithDictionary:(NSDictionary *)dic {

    

    if (self = [superinit]) {

        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 [NSStringstringWithFormat:@"userID:%@,userName:%@,userPassword:%@,userNickName:%@,HeadImg:%@,createTime:%@",self.userID,self.userName,self.userPassword,self.userNickName,self.HeadImg,self.createTime];

}


-(void)encodeWithCoder:(NSCoder *)aCoder{

    [selfyy_modelEncodeWithCoder:aCoder];

}


-(id)initWithCoder:(NSCoder *)aDecoder{

    self = [superinit];

    return [selfyy_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 = [NSUserDefaultsstandardUserDefaults];

    NSData *data = [userDefault objectForKey:@"1userObject"];

    if (data.length >0) {

        loginState = YES;

    }else{

        loginState = NO;

    }

    return loginState;

    

}


+(void)saveUserObject:(UserInfo *)userinfo{

    NSData *data = [NSKeyedArchiverarchivedDataWithRootObject:userinfo];

    NSUserDefaults *userDefault = [NSUserDefaultsstandardUserDefaults];

    [userDefault setObject:dataforKey:[NSStringstringWithFormat:@"%@userObject",@"1"]];

}


+(UserInfo *)getUserObject{

    NSUserDefaults *userDefault = [NSUserDefaultsstandardUserDefaults];

    NSData *data = [userDefault objectForKey:@"1userObject"];

    return [NSKeyedUnarchiverunarchiveObjectWithData:data ];

}


+(void)logoOut{

    [[NSUserDefaultsstandardUserDefaults] removeObjectForKey:@"1userObject"];

}



@end


、、、、、是否登录判断

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    

    if (![UserManagerisLogin]) {

        

        LoginViewController *vc = [[LoginViewControlleralloc] init];

        UINavigationController *nav = [[UINavigationControlleralloc] initWithRootViewController:vc];

        self.window.rootViewController= nav;


    } else {

        

        NSLog(@"%@",[UserManagergetUserObject]);

        UserInfo *userInfo = [UserManagergetUserObject];

        NSLog(@"%@",userInfo);

        NSLog(@"zzzzz%@",userInfo.userName);

        

        MainTabBarController *tabBar = [[MainTabBarControlleralloc] init];

        self.window.rootViewController = tabBar;

   }

    

    

    

    

    

    return YES;

}


、、、、、、、、、、、添加用户信息到本地设置  

if ([str isEqualToString:@"101"]) {

            

            [SVProgressHUDshowInfoWithStatus:@"登录成功"];

            

            for (NSDictionary *dicin [responseObject objectForKey:@"data"]) {

                

                UserInfo *userInfo = [[UserInfoalloc] 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 = [[UIApplicationsharedApplication] keyWindow];

            

            MainTabBarController *VC = [MainTabBarControlleralloc];

            

            window.rootViewController = VC;

            

            

            

            

        } else {

            [SVProgressHUD showErrorWithStatus:[responseObject objectForKey:@"msg"]];

        }





阅读全文
0 0
原创粉丝点击