简体   繁体   中英

How i can make NSUserDefaults for each user of my app?

I need make access to UserDefaults in my app. But i need save settings for each user, who was registered in app. For example:

[[NSUserDefaults standartUserDefaults] setObject:@"15:00" forKey:@"lastTime"]// for current user1

and after logout user1 and login user2:

[[NSUserDefaults standartUserDefaults] setObject:@"11:00" forKey:@"lastTime"]// for current user2

And, if i try read key @"lastTime", then i get the last value, but not for this user. I can store userDefaults in differentFiles, as an a variant? Or other solutions?

UPDATED: Finally i solved this problem:

#define UserDefaults  [NSUserDefaults standardUserDefaults]
#define uKey(key)  [NSString stringWithFormat:@"%@_%@",key,User.user_name]


[UserDefaults setInteger:lastData forKey:uKey(@"lastResultDataId")];

There is no concept of users on iOS (yet). Each device, an iPhone or iPad, is presumed to have one and only one user. Obviously that's often not true, especially on an iPad, so some apps create schemes for different people to use the device. If you've done that -- and it's not clear that you have -- then you know the username. If you are writing a Mac app then use NSUserName() to get the name of the logged-in user. After that just concatenate the username onto the key from your NSUserDefaults, something like this:

NSString *username = @"john";
[[NSUserDefaults standardUserDefaults] setObject:@"15:00" forKey:[NSString stringWithFormat:@"last_login_username_%@", username]];

(username would set either to NSUserName() for a Mac app or whatever scheme you've come up for a multiuser iOS app)

If you have some kind of user identifier, use a dictionary for the different users:

NSDictionary *user = @{@"lastTime": @"15:00" };
[[NSUserDefaults standartUserDefaults] setObject:user forKey:@"userId"];

您必须有某种方法来唯一标识用户,然后在设置/获取lastTime值时在密钥中使用UUID,用户名,电子邮件,userId等任何东西

Personally I'd use a NSDictionary like and have a dictionary per user and store that into NSUserDefaults under a key constructed out of their username similar to below.

NSMutableDictionary *userDict = [[NSMutableDictionary alloc] init];
NSString *username = "myUsername";
NSDate *currentDateTime = [NSDate date];

[userDict setObject:username forKey:@"username"];
[userDict setObject:currentDateTime forKey:@"lastAccessed"];

/* Any other user date you want to store about this user
 * Note we are going to use NSUserDefaults which is not
 * secure so do not store sensitive data in here
 */

 NSString *userDictKey = [NSString stringWithFormat:@"uk.co.yourAppName.%@", username];
 [[NSUserDefaults standardUserDefaults] setObject:userDict forKey:userDictKey];
 [[NSUserDefaults standartUserDefaults] synchronize];

And to retrieve this data all you'd need to do is know what the users username is and do

NSString *userDictKey = [NSString stringWithFormat:@"uk.co.yourAppName.%@", username];
NSDictionary *userRetrievedDict = [[NSUserDefaults standardUserDefaults] dictionaryForKey:userDictKey];

However if you want to make any amendments to this data you'll need to adapt it so that the data is moved into a NSMutableDictionary so you can update it and then store it back into NSUserDefaults

Make sure that after setting value, user going to synchronise the userdefaults. like

[[NSUserDefaults standartUserDefaults] synchronize];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM