NSUserDefault 保存对象,登录问题?

fal_con
  • 83

登录的时候我用userdefaults保存的user_id,但App关闭后台,在开启这个user_id就没了,我同样保存的telephone就在

LoginViewController.m


// NSUserDefault NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:self.phoneString forKey:@"telephone"]; [defaults [defaults setObject:self.useridString forKey:@"user_id"]; [defaults setValue:self.useridString forKey:@"user_id"]; [defaults setValue:@"YES" forKey:@"hadLogin"]; [defaults setValue:@"YES" forKey:@"guest"]; [defaults synchronize]; NSMutableDictionary *userInfoD = [[NSMutableDictionary alloc] init]; [userInfoD setObject:self.phoneString forKey:@"telephone"]; [userInfoD setObject:self.passwordString forKey:@"password"]; [userInfoD setValue:self.useridString forKey:@"user_id"]; [userInfoD setValue:@"YES" forKey:@"hadLogin"]; [userInfoD setValue:@"YES" forKey:@"guest"]; [User sharedInstance].userinfoDict = userInfoD;

MoreViewController里面有个LogOutButton,当我点击LogOut我就把单例里面的hadLogin=NO


这是我的Modal
这是我的Modal

//  User.m
#import "User.h"

@implementation User
@synthesize userinfoDict = _userinfoDict;
@synthesize userType=_userType;

- (id)init
{
    self = [super init];
    if (self)
    {
        NSMutableDictionary *userInfoDict = [[NSMutableDictionary alloc]init];
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        //-------------------------------------------------------------------------------------
        [userInfoDict setValue:[userDefaults stringForKey:@"user_id"] forKey:@"user_id"];

        [userInfoDict setValue:[userDefaults stringForKey:@"collection_interest_total"] forKey:@"collection_interest_total"];
        [userInfoDict setValue:[userDefaults stringForKey:@"collection_interest_wait"] forKey:@"collection_interest_wait"];
        [userInfoDict setValue:[userDefaults stringForKey:@"collection_total_wait"] forKey:@"collection_total_wait"];
        [userInfoDict setValue:[userDefaults stringForKey:@"hongbao"] forKey:@"hongbao"];
        [userInfoDict setValue:[userDefaults stringForKey:@"totalmoney"] forKey:@"totalmoney"];
        [userInfoDict setValue:[userDefaults stringForKey:@"use_money"] forKey:@"use_money"];
        [userInfoDict setValue:[userDefaults stringForKey:@"yesterday_money"] forKey:@"yesterday_money"];
        [userInfoDict setValue:[userDefaults stringForKey:@"yq_reward"] forKey:@"yq_reward"];
        [userInfoDict setValue:[userDefaults stringForKey:@"phone"] forKey:@"phone"];
        [userInfoDict setValue:[userDefaults stringForKey:@"username"] forKey:@"username"];

        //**是否登录状态**//
        [userInfoDict setValue:[userDefaults stringForKey:@"loginState"] forKey:@"loginState"];
        [userInfoDict setValue:[userDefaults stringForKey:@"hadLogin"] forKey:@"hadLogin"];





        self.userinfoDict =userInfoDict;
        self.userUnreadMessage = [[NSMutableArray alloc]  init];
        NSLog(@"Model.m_userInfoDict ==== %@",userInfoDict);
    }
    return self;
}


- (void)setUserinfoDict:(NSMutableDictionary *)userinfoDict
{
    _userinfoDict = userinfoDict;
    [self loadAvatarImag];
}

+ (User *)sharedInstance
{
    static User *_sharedInstance = nil;
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        _sharedInstance = [[User alloc] init];
    });
    return _sharedInstance;
}

- (void)loadAvatarImag
{
    if (![[self.userinfoDict objectForKey:@"avatar"] isKindOfClass:[NSString class]]) {
        return;
    }
    if (![[self.userinfoDict objectForKey:@"avatar"] isKindOfURL]) {
        return;
    }

}

-(void)reloadUserInfo:(NSDictionary *)responseObject   hadLogin:(NSString *)hadLogin  isGuest:(NSString  *)isGuest{



    NSString  *hadLogin_old= [[User sharedInstance].userinfoDict  objectForKey:@"hadLogin"];
    NSString  *isGuest_old= [[User sharedInstance].userinfoDict  objectForKey:@"guest"];
    NSString  *hadLogin_new=hadLogin_old;
    NSString  *isGuest_new=isGuest_old;
    if (hadLogin) {
        hadLogin_new =hadLogin;
    }
    if (isGuest) {
        isGuest_new =isGuest;
    }

    NSLog(@"rrrrrrrrrresponseObject = %@", responseObject);


//    responseObject = [responseObject  objectForKey:@"data"];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

    [userDefaults setValue:[NSString  stringWithFormat:@"%@",[responseObject valueForKey:@"user_id"]] forKey:@"user_id"];


    [userDefaults setValue:[responseObject valueForKey:@"collection_interest_total"] forKey:@"collection_interest_total"];
    [userDefaults setValue:[responseObject valueForKey:@"collection_interest_wait"] forKey:@"collection_interest_wait"];
    [userDefaults setValue:[responseObject valueForKey:@"collection_total_wait"] forKey:@"collection_total_wait"];
    [userDefaults setValue:[responseObject valueForKey:@"hongbao"] forKey:@"hongbao"];
    [userDefaults setValue:[responseObject valueForKey:@"totalmoney"] forKey:@"totalmoney"];
    [userDefaults setValue:[responseObject valueForKey:@"use_money"] forKey:@"use_money"];
    [userDefaults setValue:[responseObject valueForKey:@"yesterday_money"] forKey:@"yesterday_money"];
    [userDefaults setValue:[responseObject valueForKey:@"yq_reward"] forKey:@"yq_reward"];

    [userDefaults setValue:[responseObject valueForKey:@"phone"] forKey:@"phone"];
    [userDefaults setValue:[responseObject valueForKey:@"username"] forKey:@"username"];
//    [userDefaults setValue:[responseObject valueForKey:@"user_id"] forKey:@"user_id"];


    //**是否登录状态**//
    [userDefaults setValue:[responseObject valueForKey:@"loginState"] forKey:@"loginState"];
    [userDefaults setValue:hadLogin_new forKey:@"hadLogin"];
    [userDefaults setValue:isGuest_new forKey:@"guest"];

    NSLog(@"User_forkey_user_id = %@", [responseObject valueForKey:@"user_id"]);






    [userDefaults synchronize];

    NSMutableDictionary *userInfoD = [[NSMutableDictionary alloc]init];
    [userInfoD setValue:[NSString  stringWithFormat:@"%@",[responseObject valueForKey:@"user_id"]] forKey:@"user_id"];




    [userInfoD setValue:[responseObject valueForKey:@"collection_interest_total"] forKey:@"collection_interest_total"];
    [userInfoD setValue:[responseObject valueForKey:@"collection_interest_wait"] forKey:@"collection_interest_wait"];
    [userInfoD setValue:[responseObject valueForKey:@"collection_total_wait"] forKey:@"collection_total_wait"];
    [userInfoD setValue:[responseObject valueForKey:@"hongbao"] forKey:@"hongbao"];
    [userInfoD setValue:[responseObject valueForKey:@"totalmoney"] forKey:@"totalmoney"];
    [userInfoD setValue:[responseObject valueForKey:@"use_money"] forKey:@"use_money"];
    [userInfoD setValue:[responseObject valueForKey:@"yesterday_money"] forKey:@"yesterday_money"];
    [userInfoD setValue:[responseObject valueForKey:@"yq_reward"] forKey:@"yq_reward"];

    [userInfoD setValue:[responseObject valueForKey:@"phone"] forKey:@"phone"];
    [userInfoD setValue:[responseObject valueForKey:@"username"] forKey:@"username"];
//    [userInfoD setValue:[responseObject valueForKey:@"user_id"] forKey:@"user_id"];

    //**是否登录状态**//
    [userInfoD setValue:[responseObject valueForKey:@"loginState"] forKey:@"loginState"];
    [userInfoD setValue:hadLogin_new forKey:@"hadLogin"];
    [userInfoD setValue:isGuest_new forKey:@"guest"];

    [User  sharedInstance].userinfoDict = userInfoD;

    NSLog(@"userInfoDDDDD = %@", userInfoD);
    NSLog(@"User  sharedInstance].userinfoDict = %@",[User  sharedInstance].userinfoDict);

}

@end

然后我在MyPropertyViewController里面调取userdefaults的user_id,代码如下

NSUserDefaults *myuserDefaults = [NSUserDefaults standardUserDefaults];
    if ([myuserDefaults objectForKey:@"user_id"]) {
        uid = [NSString stringWithFormat:@"%@", [myuserDefaults objectForKey:@"user_id"]];
    }
    else
    {
        uid = [NSString stringWithFormat:@"%@", [[User sharedInstance].userinfoDict objectForKey:@"user_id"]];
    }

回复
阅读 5.2k
2 个回答

为测试,可以自己尝试一下

可能是setValue引起,我也遇到过,不过已经很久了,是不是你可以自己改正下看:

LoginViewController.m

你的代码针对user_id进行了两次赋值,如下

[defaults setObject:self.useridString forKey:@"user_id"];
[defaults setValue:self.useridString forKey:@"user_id"];

建议删除setValue:self

[defaults setObject:self.useridString forKey:@"user_id"];

如果是这个原因,建议阅读setValue和setObject的区别

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏