iOS开发之功能模块--本地序列化
下面只展示项目开发中,本地序列化的示例代码:
AuthenticationManager.h
1 #import <Foundation/Foundation.h> 2 #import "GrantViewController.h" 3 #import "MUUser.h" 4 #import "RoomEntity.h" 5 6 #define kNotificationLogoutDone @"kNotification_logout_done" 7 #define kScoreChangeNotification @"kScoreChangeNotification" //金币变化 8 #define kExpriedTimeChangeNotification @"kExpriedTimeChangeNotification" //VIP时间变化 9 #define kRoomUpdateNotification @"kRoomUpdateNotification" //房间更新 10 11 @class Topic; 12 @class PlayableItem; 13 14 @interface AuthenticationManager : NSObject 15 16 + (MUUser *)authenticatedUser; 17 + (BOOL)isAuthenticated; 18 + (void)logout; 19 + (void)updateUserDetail:(NSDictionary *)data; 20 + (void)updateUserInfo:(MUUser *)user; 21 + (void)updateUserAccessToken:(NSString *)token; 22 + (void)addUnPublicTopicUserInfo:(NSMutableDictionary *)userInfo forKey:(NSString *)key; 23 + (void)removeUnPublicTopicUserInfo:(NSString *)key; 24 + (void)finishPresent; 25 + (void)updateLatestTopicDate:(NSDate *)date; 26 + (void)updateLatestActivityDate:(NSDate *)date; 27 + (void)updateLatestSystemMessageDate:(NSDate *)date; 28 + (void)updateLatestCheckFollowsDate:(NSDate *)date ; 29 + (void)updateAutoPlaySetting:(NSInteger)setting; 30 + (void)updateLastPlayTopic:(Topic *)topic; 31 + (void)updateUserScore:(NSInteger)score; 32 + (void)updateExpriedTime:(NSNumber *)time; 33 + (void)updateRoom:(RoomEntity *)room; 34 + (void)syncUserInfo; 35 36 + (AuthenticationManager *)instance; 37 38 39 @end
1 #import "NSMutableDictionary+Additions.h" 2 #import "XXUserManager.h" 3 4 @interface AuthenticationManager() 5 6 @property (nonatomic, strong) MusicEngine *dataEngine; 7 8 @end 9 10 @implementation AuthenticationManager 11 12 + (void)logout{ 13 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kUserID]; 14 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kUsername]; 15 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kPassword]; 16 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kSalt]; 17 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kEmail]; 18 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kAvatar]; 19 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kDisplayName]; 20 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kScore]; 21 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kToken]; 22 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kOpenID]; 23 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kUserSocialAccount]; 24 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kDiangeType]; 25 26 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kUserCover]; 27 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kUserSign]; 28 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kUserFollowings]; 29 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kUserFollows]; 30 31 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kUnPublicTopicUserInfo]; 32 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kPresentBefore]; 33 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kLatestTopicDate]; 34 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kLatestActivityDate]; 35 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kLatestSystemMessageDate]; 36 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kLatestCheckFollowsDate]; 37 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kAutoPlaySetting]; 38 39 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kGender]; 40 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kLocation]; 41 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kBirthday]; 42 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kUserLastPlayTopic]; 43 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kExpriedTime]; 44 45 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kMoives]; 46 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kBooks]; 47 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kMusics]; 48 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kPets]; 49 50 [[NSUserDefaults standardUserDefaults] setBool:NO forKey:kCheckUploadPhotoTaskStatus]; 51 52 [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kRoom]; 53 54 [[NSUserDefaults standardUserDefaults] synchronize]; 55 56 [[NSURLCache sharedURLCache] removeAllCachedResponses]; 57 for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) { 58 [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie]; 59 } 60 61 AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; 62 [appDelegate.sinaWeiboOAuth logOut]; 63 [appDelegate.tencentOAuth logout:nil]; 64 } 65 66 + (MUUser *)authenticatedUser { 67 if (![AuthenticationManager isAuthenticated]) { 68 DLog(@"用户未登录啊。"); 69 return nil; 70 } 71 MUUser *user = [[MUUser alloc] init]; 72 73 user.username = [[NSUserDefaults standardUserDefaults] objectForKey:kUsername]; 74 user.password = [[NSUserDefaults standardUserDefaults] objectForKey:kPassword]; 75 user.salt = [[NSUserDefaults standardUserDefaults] objectForKey:kSalt]; 76 user.firstName = [[NSUserDefaults standardUserDefaults] objectForKey:kDisplayName]; 77 user.email = [[NSUserDefaults standardUserDefaults] objectForKey:kEmail]; 78 user.avatar = [[NSUserDefaults standardUserDefaults] objectForKey:kAvatar]; 79 user.score = [[[NSUserDefaults standardUserDefaults] objectForKey:kScore] integerValue]; 80 user.token = [[NSUserDefaults standardUserDefaults] objectForKey:kToken]; 81 user.openID = [[NSUserDefaults standardUserDefaults] objectForKey:kOpenID]; 82 user.userID = [[NSUserDefaults standardUserDefaults] integerForKey:kUserID]; 83 84 user.cover = [[NSUserDefaults standardUserDefaults] objectForKey:kUserCover]; 85 user.sign = [[NSUserDefaults standardUserDefaults] objectForKey:kUserSign]; 86 user.followings = [[NSUserDefaults standardUserDefaults] integerForKey:kUserFollowings]; 87 user.followers = [[NSUserDefaults standardUserDefaults] integerForKey:kUserFollows]; 88 89 user.isPresentBefore = [[NSUserDefaults standardUserDefaults] boolForKey:kPresentBefore]; 90 user.latestTopicDate = [[NSUserDefaults standardUserDefaults] objectForKey:kLatestTopicDate]; 91 user.latestActivityDate = [[NSUserDefaults standardUserDefaults] objectForKey:kLatestActivityDate]; 92 user.latestSystemMessageDate = [[NSUserDefaults standardUserDefaults] objectForKey:kLatestSystemMessageDate]; 93 user.latestCheckFollowsDate = [[NSUserDefaults standardUserDefaults] objectForKey:kLatestCheckFollowsDate]; 94 user.deadline = [[NSUserDefaults standardUserDefaults] objectForKey:kExpriedTime]; 95 96 NSData *moviesData = [[NSUserDefaults standardUserDefaults] objectForKey:kMoives]; 97 user.movies = [NSKeyedUnarchiver unarchiveObjectWithData: moviesData]; 98 99 NSData *booksData = [[NSUserDefaults standardUserDefaults] objectForKey:kBooks]; 100 user.books = [NSKeyedUnarchiver unarchiveObjectWithData: booksData]; 101 102 NSData *musicsData = [[NSUserDefaults standardUserDefaults] objectForKey:kMusics]; 103 user.musics = [NSKeyedUnarchiver unarchiveObjectWithData: musicsData]; 104 105 NSData *petsData = [[NSUserDefaults standardUserDefaults] objectForKey:kPets]; 106 user.pets = [NSKeyedUnarchiver unarchiveObjectWithData: petsData]; 107 108 id autoPlaySetting = [[NSUserDefaults standardUserDefaults] objectForKey:kAutoPlaySetting]; 109 if (autoPlaySetting) 110 user.autoPlaySetting = (enum AutoPlayType) [autoPlaySetting integerValue]; 111 112 user.gender = [[NSUserDefaults standardUserDefaults] integerForKey:kGender]; 113 user.location = [[NSUserDefaults standardUserDefaults] objectForKey:kLocation]; 114 user.birthday = [[NSUserDefaults standardUserDefaults] objectForKey:kBirthday]; 115 user.areaCode = [[NSUserDefaults standardUserDefaults] objectForKey:kAreaCode]; 116 117 NSData *roomData = [[NSUserDefaults standardUserDefaults] objectForKey:kRoom]; 118 user.room = [NSKeyedUnarchiver unarchiveObjectWithData:roomData]; 119 120 NSDictionary *unPublicTopicUserInfo = [[NSUserDefaults standardUserDefaults] objectForKey:kUnPublicTopicUserInfo]; 121 if (!unPublicTopicUserInfo) 122 user.unPublicTopicUserInfo = [NSMutableDictionary dictionary]; 123 else 124 user.unPublicTopicUserInfo = [NSMutableDictionary dictionaryWithDictionary:unPublicTopicUserInfo]; 125 126 NSData *myEncodedObject = [[NSUserDefaults standardUserDefaults] objectForKey:kUserSocialAccount]; 127 user.social_accounts = [NSKeyedUnarchiver unarchiveObjectWithData: myEncodedObject]; 128 if (user.social_accounts == nil) { 129 // 原来是有QQ帐号的呢。搞一个过去。 130 SocialAccount *account = [[SocialAccount alloc] init]; 131 account.identity = [[NSUserDefaults standardUserDefaults] objectForKey:kOpenID]; 132 account.type = SocialAccountQQ; 133 account.provider = @"qq"; 134 account.isMaster = YES; 135 account.name = [[NSUserDefaults standardUserDefaults] objectForKey:kDisplayName]; 136 account.accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:kToken]; 137 user.social_accounts = @[account]; 138 } 139 140 NSDictionary *topicDict = [[NSUserDefaults standardUserDefaults] objectForKey:kUserLastPlayTopic]; 141 142 MUUser *owner = [[MUUser alloc] init]; 143 owner.userID = [topicDict[@"userID"] integerValue]; 144 owner.username = topicDict[@"userName"]; 145 owner.firstName = topicDict[@"firstName"]; 146 147 Topic *topic = [[Topic alloc] init]; 148 topic.topicID = [topicDict[@"topicID"] integerValue]; 149 topic.name = topicDict[@"topicName"]; 150 topic.cover = topicDict[@"topicCover"]; 151 topic.owner = owner; 152 user.lastPlayTopic = topic; 153 154 return user; 155 } 156 157 + (BOOL)isAuthenticated { 158 159 return [XXUserManager isAuth]; 160 161 NSString *username = [[NSUserDefaults standardUserDefaults] objectForKey:kUsername]; 162 NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:kPassword]; 163 return username && password ? YES : NO; 164 } 165 166 + (void)updateUserDetail:(NSDictionary *)data{ 167 [[NSUserDefaults standardUserDefaults] setObject:data[@"u"] forKey:kUsername]; 168 [[NSUserDefaults standardUserDefaults] setObject:data[@"m"] forKey:kEmail]; 169 [[NSUserDefaults standardUserDefaults] setObject:data[@"p"] forKey:kPassword]; 170 [[NSUserDefaults standardUserDefaults] setObject:data[@"s"] forKey:kSalt]; 171 [[NSUserDefaults standardUserDefaults] setObject:data[@"d"] forKey:kDisplayName]; 172 [[NSUserDefaults standardUserDefaults] setObject:data[@"t"] forKey:kToken]; 173 [[NSUserDefaults standardUserDefaults] setObject:data[@"i"] forKey:kOpenID]; 174 [[NSUserDefaults standardUserDefaults] setObject:data[@"h"] forKey:kAvatar]; 175 [[NSUserDefaults standardUserDefaults] setInteger:[data[@"id"] integerValue] forKey:kUserID]; 176 [[NSUserDefaults standardUserDefaults] synchronize]; 177 } 178 179 + (void)updateUserInfo:(MUUser *)user{ 180 [[NSUserDefaults standardUserDefaults] setObject:user.username forKey:kUsername]; 181 [[NSUserDefaults standardUserDefaults] setObject:user.email forKey:kEmail]; 182 [[NSUserDefaults standardUserDefaults] setObject:user.firstName forKey:kDisplayName]; 183 [[NSUserDefaults standardUserDefaults] setObject:user.avatar forKey:kAvatar]; 184 [[NSUserDefaults standardUserDefaults] setInteger:user.userID forKey:kUserID]; 185 [[NSUserDefaults standardUserDefaults] setObject:user.cover forKey:kUserCover]; 186 [[NSUserDefaults standardUserDefaults] setObject:user.sign forKey:kUserSign]; 187 [[NSUserDefaults standardUserDefaults] setInteger:user.followers forKey:kUserFollows]; 188 [[NSUserDefaults standardUserDefaults] setInteger:user.followings forKey:kUserFollowings]; 189 [[NSUserDefaults standardUserDefaults] setInteger:user.score forKey:kScore]; 190 [[NSUserDefaults standardUserDefaults] setInteger:user.gender forKey:kGender]; 191 [[NSUserDefaults standardUserDefaults] setObject:user.location forKey:kLocation]; 192 [[NSUserDefaults standardUserDefaults] setObject:user.birthday forKey:kBirthday]; 193 [[NSUserDefaults standardUserDefaults] setObject:user.deadline forKey:kExpriedTime]; 194 195 NSData *moviesData = [NSKeyedArchiver archivedDataWithRootObject:user.movies]; 196 [[NSUserDefaults standardUserDefaults] setObject:moviesData forKey:kMoives]; 197 198 NSData *booksData = [NSKeyedArchiver archivedDataWithRootObject:user.books]; 199 [[NSUserDefaults standardUserDefaults] setObject:booksData forKey:kBooks]; 200 201 NSData *musicData = [NSKeyedArchiver archivedDataWithRootObject:user.musics]; 202 [[NSUserDefaults standardUserDefaults] setObject:musicData forKey:kMusics]; 203 204 NSData *petsData = [NSKeyedArchiver archivedDataWithRootObject:user.pets]; 205 [[NSUserDefaults standardUserDefaults] setObject:petsData forKey:kPets]; 206 207 NSData *roomData = [NSKeyedArchiver archivedDataWithRootObject:user.room]; 208 [[NSUserDefaults standardUserDefaults] setObject:roomData forKey:kRoom]; 209 210 [[NSUserDefaults standardUserDefaults] setObject:user.areaCode forKey:kAreaCode]; 211 212 NSData *archiveCarPriceData = [NSKeyedArchiver archivedDataWithRootObject:user.social_accounts]; 213 [[NSUserDefaults standardUserDefaults] setObject:archiveCarPriceData forKey:kUserSocialAccount]; 214 215 if (user.password && ![user.password isEqualToString:@""]) { 216 if ([[NSUserDefaults standardUserDefaults] objectForKey:kPassword] == nil) { 217 [[NSUserDefaults standardUserDefaults] setObject:user.password forKey:kPassword]; 218 } 219 } 220 221 if (user.salt && ![user.salt isEqualToString:@""]) { 222 if ([[NSUserDefaults standardUserDefaults] objectForKey:kSalt] == nil) { 223 [[NSUserDefaults standardUserDefaults] setObject:user.salt forKey:kSalt]; 224 } 225 } 226 [[NSUserDefaults standardUserDefaults] synchronize]; 227 } 228 229 + (void)updateUserAccessToken:(NSString *)token { 230 [[NSUserDefaults standardUserDefaults] setObject:token forKey:kToken]; 231 [[NSUserDefaults standardUserDefaults] synchronize]; 232 } 233 234 + (void)addUnPublicTopicUserInfo:(NSMutableDictionary *)userInfo forKey:(NSString *)key { 235 NSDictionary *topicUserInfo = [[NSUserDefaults standardUserDefaults] objectForKey:kUnPublicTopicUserInfo]; 236 NSMutableDictionary *unPublicTopicUserInfo; 237 if (!topicUserInfo) 238 unPublicTopicUserInfo = [NSMutableDictionary dictionary]; 239 else 240 unPublicTopicUserInfo = [NSMutableDictionary dictionaryWithDictionary:topicUserInfo]; 241 unPublicTopicUserInfo[key] = userInfo; 242 [[NSUserDefaults standardUserDefaults] setObject:unPublicTopicUserInfo forKey:kUnPublicTopicUserInfo]; 243 [[NSUserDefaults standardUserDefaults] synchronize]; 244 } 245 246 + (void)removeUnPublicTopicUserInfo:(NSString *)key { 247 NSDictionary *topicUserInfo = [[NSUserDefaults standardUserDefaults] objectForKey:kUnPublicTopicUserInfo]; 248 if (!topicUserInfo) 249 return; 250 251 NSMutableDictionary *unPublicTopicUserInfo = [NSMutableDictionary dictionaryWithDictionary:topicUserInfo]; 252 [unPublicTopicUserInfo removeObjectForKey:key]; 253 [[NSUserDefaults standardUserDefaults] setObject:unPublicTopicUserInfo forKey:kUnPublicTopicUserInfo]; 254 [[NSUserDefaults standardUserDefaults] synchronize]; 255 } 256 257 + (void)finishPresent { 258 [[NSUserDefaults standardUserDefaults] setObject:@YES forKey:kPresentBefore]; 259 [[NSUserDefaults standardUserDefaults] synchronize]; 260 } 261 262 + (void)updateLatestTopicDate:(NSDate *)date { 263 [[NSUserDefaults standardUserDefaults] setObject:date forKey:kLatestTopicDate]; 264 [[NSUserDefaults standardUserDefaults] synchronize]; 265 } 266 267 + (void)updateLatestActivityDate:(NSDate *)date { 268 [[NSUserDefaults standardUserDefaults] setObject:date forKey:kLatestActivityDate]; 269 [[NSUserDefaults standardUserDefaults] synchronize]; 270 } 271 272 + (void)updateLatestSystemMessageDate:(NSDate *)date { 273 [[NSUserDefaults standardUserDefaults] setObject:date forKey:kLatestSystemMessageDate]; 274 [[NSUserDefaults standardUserDefaults] synchronize]; 275 } 276 277 + (void)updateLatestCheckFollowsDate:(NSDate *)date { 278 [[NSUserDefaults standardUserDefaults] setObject:date forKey:kLatestCheckFollowsDate]; 279 [[NSUserDefaults standardUserDefaults] synchronize]; 280 } 281 282 + (void)updateAutoPlaySetting:(NSInteger)setting { 283 [[NSUserDefaults standardUserDefaults] setObject:@(setting) forKey:kAutoPlaySetting]; 284 [[NSUserDefaults standardUserDefaults] synchronize]; 285 } 286 287 + (void)updateLastPlayTopic:(Topic *)topic { 288 NIDPRINT(@"更新最后播放的节目信息: %@", topic); 289 NSMutableDictionary *dict = [NSMutableDictionary dictionary]; 290 [dict safeSetObject:@(topic.topicID) forKey:@"topicID"]; 291 [dict safeSetObject:topic.name forKey:@"topicName"]; 292 [dict safeSetObject:topic.cover forKey:@"topicCover"]; 293 [dict safeSetObject:@(topic.owner.userID) forKey:@"userID"]; 294 [dict safeSetObject:topic.owner.firstName forKey:@"firstName"]; 295 [dict safeSetObject:topic.owner.username forKey:@"userName"]; 296 [[NSUserDefaults standardUserDefaults] setObject:dict forKey:kUserLastPlayTopic]; 297 [[NSUserDefaults standardUserDefaults] synchronize]; 298 } 299 300 + (void)updateUserScore:(NSInteger)score { 301 [[NSUserDefaults standardUserDefaults] setObject:@(score) forKey:kScore]; 302 [[NSUserDefaults standardUserDefaults] synchronize]; 303 [[NSNotificationCenter defaultCenter] postNotificationName:kScoreChangeNotification object:self userInfo:nil]; 304 } 305 306 + (void)updateExpriedTime:(NSNumber *)time { 307 [[NSUserDefaults standardUserDefaults] setObject:time forKey:kExpriedTime]; 308 [[NSUserDefaults standardUserDefaults] synchronize]; 309 [[NSNotificationCenter defaultCenter] postNotificationName:kExpriedTimeChangeNotification object:[self authenticatedUser] userInfo:nil]; 310 } 311 312 + (void)updateRoom:(RoomEntity *)room { 313 NSData *roomData = [NSKeyedArchiver archivedDataWithRootObject:room]; 314 [[NSUserDefaults standardUserDefaults] setObject:roomData forKey:kRoom]; 315 [[NSNotificationCenter defaultCenter] postNotificationName:kRoomUpdateNotification object:[self authenticatedUser] userInfo:nil]; 316 } 317 318 + (void)syncUserInfo { 319 MUUser *oldUser = [AuthenticationManager authenticatedUser]; 320 if (!oldUser) 321 return; 322 323 @weakify(self) 324 [[self instance].dataEngine loadUserByUserId:oldUser.userID success:^(MUUser *userEntity, NSDictionary *userInfo) { 325 NIDPRINT(@"旧金币:%ld 新金币:%ld", (long)oldUser.score,(long)userEntity.score); 326 [AuthenticationManager updateUserInfo:userEntity]; 327 @strongify(self) 328 if (oldUser.score != userEntity.score) { 329 [[NSNotificationCenter defaultCenter] postNotificationName:kScoreChangeNotification object:self userInfo:nil]; 330 } 331 if (oldUser.deadline.integerValue != userEntity.deadline.integerValue) { 332 [[NSNotificationCenter defaultCenter] postNotificationName:kExpriedTimeChangeNotification object:self userInfo:nil]; 333 } 334 } failure:^(id result, NSDictionary *userInfo) { 335 336 }]; 337 } 338 339 + (AuthenticationManager *)instance { 340 static AuthenticationManager *_instance = nil; 341 342 @synchronized (self) { 343 if (_instance == nil) { 344 _instance = [[self alloc] init]; 345 } 346 } 347 348 return _instance; 349 } 350 351 - (MusicEngine *)dataEngine { 352 if (!_dataEngine) { 353 _dataEngine = [[MusicEngine alloc] init]; 354 } 355 return _dataEngine; 356 } 357 358 359 @end
第二个示例:XXUserManager
1 #import <Foundation/Foundation.h> 2 #import "XXUserModel.h" 3 4 @interface XXUserManager : NSObject 5 6 + (BOOL)isAuth; 7 8 + (XXUserModel *)authUser; 9 10 + (void)updateAuthUser:(XXUserModel *)authUser; 11 12 + (void)updateUserInfo:(XXUserModel *)user; 13 14 + (void)logout; 15 16 @end
1 #import "XXUserManager.h" 2 3 #define kXXUserId @"xxUserid" 4 #define kXXUserName @"xxUsername" 5 #define kXXSalt @"xxSalt" 6 #define kXXSecret @"xxSecret" 7 #define kXXMobile @"xxMobile" 8 #define kXXNickName @"xxNickname" 9 #define kXXAvatar @"xxAvatar" 10 #define kXXSex @"xxSex" 11 #define kXXBirthday @"xxBirthday" 12 #define kXXLocation @"xxLocation" 13 #define kXXSign @"xxSign" 14 #define kXXFollowers @"xxFollowers" 15 #define kXXFollowings @"xxFollowings" 16 #define kXXEarning @"xxEarning" 17 #define kXXScores @"xxScores" 18 #define kXXTimUsersig @"xxTimusersig" 19 #define kXXValue @"kXXValue" 20 21 @implementation XXUserManager 22 23 + (XXUserModel *)authUser { 24 25 XXUserModel *autnUser = [[XXUserModel alloc] init]; 26 27 autnUser.userId = [[NSUserDefaults standardUserDefaults] objectForKey:kXXUserId]; 28 autnUser.username = [[NSUserDefaults standardUserDefaults] objectForKey:kXXUserName]; 29 autnUser.salt = [[NSUserDefaults standardUserDefaults] objectForKey:kXXSalt]; 30 autnUser.secret = [[NSUserDefaults standardUserDefaults] objectForKey:kXXSecret]; 31 autnUser.mobile = [[NSUserDefaults standardUserDefaults] objectForKey:kXXMobile]; 32 autnUser.nickname = [[NSUserDefaults standardUserDefaults] objectForKey:kXXNickName]; 33 autnUser.avatar = [[NSUserDefaults standardUserDefaults] objectForKey:kXXAvatar]; 34 autnUser.sex = [[NSUserDefaults standardUserDefaults] integerForKey:kXXSex]; 35 autnUser.birthday = [[NSUserDefaults standardUserDefaults] objectForKey:kXXBirthday]; 36 autnUser.location = [[NSUserDefaults standardUserDefaults] objectForKey:kXXLocation]; 37 autnUser.sign = [[NSUserDefaults standardUserDefaults] objectForKey:kXXSign]; 38 autnUser.followers = [[NSUserDefaults standardUserDefaults] integerForKey:kXXFollowers]; 39 autnUser.followings = [[NSUserDefaults standardUserDefaults] in8tegerForKey:kXXFollowings]; 40 autnUser.earning = [[NSUserDefaults standardUserDefaults] integerForKey:kXXEarning]; 41 autnUser.value = [[NSUserDefaults standardUserDefaults] integerForKey:kXXValue]; 42 43 NSData *scoresData = [[NSUserDefaults standardUserDefaults] objectForKey:kXXScores]; 44 autnUser.scores = [NSKeyedUnarchiver unarchiveObjectWithData:scoresData]; 45 46 autnUser.timUsersig = [[NSUserDefaults standardUserDefaults] objectForKey:kXXTimUsersig]; 47 48 return autnUser; 49 } 50 51 + (void)updateAuthUser:(XXUserModel *)authUser { 52 53 [[NSUserDefaults standardUserDefaults] setObject:authUser.userId forKey:kXXUserId]; 54 [[NSUserDefaults standardUserDefaults] setObject:authUser.username forKey:kXXUserName]; 55 [[NSUserDefaults standardUserDefaults] setObject:authUser.salt forKey:kXXSalt]; 56 [[NSUserDefaults standardUserDefaults] setObject:authUser.secret forKey:kXXSecret]; 57 [[NSUserDefaults standardUserDefaults] setObject:authUser.mobile forKey:kXXMobile]; 58 [[NSUserDefaults standardUserDefaults] setObject:authUser.nickname forKey:kXXNickName]; 59 [[NSUserDefaults standardUserDefaults] setObject:authUser.avatar forKey:kXXAvatar]; 60 [[NSUserDefaults standardUserDefaults] setInteger:authUser.sex forKey:kXXSex]; 61 [[NSUserDefaults standardUserDefaults] setObject:authUser.birthday forKey:kXXBirthday]; 62 [[NSUserDefaults standardUserDefaults] setObject:authUser.location forKey:kXXLocation]; 63 [[NSUserDefaults standardUserDefaults] setObject:authUser.sign forKey:kXXSign]; 64 [[NSUserDefaults standardUserDefaults] setInteger:authUser.followers forKey:kXXFollowers]; 65 [[NSUserDefaults standardUserDefaults] setInteger:authUser.followings forKey:kXXFollowings]; 66 [[NSUserDefaults standardUserDefaults] setInteger:authUser.earning forKey:kXXEarning]; 67 68 NSData *scoresData = [NSKeyedArchiver archivedDataWithRootObject:authUser.scores]; 69 [[NSUserDefaults standardUserDefaults] setObject:scoresData forKey:kXXScores]; 70 71 [[NSUserDefaults standardUserDefaults] setObject:authUser.timUsersig forKey:kXXTimUsersig]; 72 [[NSUserDefaults standardUserDefaults] synchronize]; 73 74 } 75 76 77 + (void)updateUserInfo:(XXUserModel *)user { 78 79 if (user.nickname) { 80 [[NSUserDefaults standardUserDefaults] setObject:user.nickname forKey:kXXNickName]; 81 } 82 if (user.avatar) { 83 [[NSUserDefaults standardUserDefaults] setObject:user.avatar forKey:kXXAvatar]; 84 } 85 86 if (user.sex) { 87 [[NSUserDefaults standardUserDefaults] setInteger:user.sex forKey:kXXSex]; 88 } 89 90 if (user.birthday) { 91 [[NSUserDefaults standardUserDefaults] setObject:user.birthday forKey:kXXBirthday]; 92 } 93 94 if (user.location) { 95 [[NSUserDefaults standardUserDefaults] setObject:user.location forKey:kXXLocation]; 96 } 97 98 if (user.sign) { 99 [[NSUserDefaults standardUserDefaults] setObject:user.sign forKey:kXXSign]; 100 } 101 [[NSUserDefaults standardUserDefaults] synchronize]; 102 103 } 104 105 + (void)logout{ 106 107 [[NSUserDefaults standardUserDefaults] removeObjectForKey:kXXUserId]; 108 [[NSUserDefaults standardUserDefaults] removeObjectForKey:kXXUserName]; 109 [[NSUserDefaults standardUserDefaults] removeObjectForKey:kXXSalt]; 110 [[NSUserDefaults standardUserDefaults] removeObjectForKey:kXXSecret]; 111 [[NSUserDefaults standardUserDefaults] removeObjectForKey:kXXMobile]; 112 [[NSUserDefaults standardUserDefaults] removeObjectForKey:kXXNickName]; 113 [[NSUserDefaults standardUserDefaults] removeObjectForKey:kXXAvatar]; 114 [[NSUserDefaults standardUserDefaults] removeObjectForKey:kXXSex]; 115 [[NSUserDefaults standardUserDefaults] removeObjectForKey:kXXBirthday]; 116 [[NSUserDefaults standardUserDefaults] removeObjectForKey:kXXLocation]; 117 [[NSUserDefaults standardUserDefaults] removeObjectForKey:kXXSign]; 118 [[NSUserDefaults standardUserDefaults] removeObjectForKey:kXXFollowers]; 119 [[NSUserDefaults standardUserDefaults] removeObjectForKey:kXXFollowings]; 120 [[NSUserDefaults standardUserDefaults] removeObjectForKey:kXXEarning]; 121 [[NSUserDefaults standardUserDefaults] removeObjectForKey:kXXScores]; 122 [[NSUserDefaults standardUserDefaults] removeObjectForKey:kXXTimUsersig]; 123 [[NSUserDefaults standardUserDefaults] removeObjectForKey:kXXValue]; 124 [[NSUserDefaults standardUserDefaults] synchronize]; 125 126 } 127 128 + (BOOL)isAuth { 129 NSString *username = [[NSUserDefaults standardUserDefaults] objectForKey:kXXUserName]; 130 NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:kXXSecret]; 131 return username && password ? YES : NO; 132 } 133 134 @end