IOS single case and NSUserDefaults mixed use


  • Store some user configuration information or some other public information we back to using a single example to do in our iOS project, but after the end of life app cases all cases of single attribute will disappear, it is not very suitable for our needs, for example, we want to record a login or user the configuration information this information is always there must also exist, then we have a single case insufficient, so I studied a single case and NSUserDefaults use case.

Realization principle:

  • First, we implement a singleton class and a Models attribute is used to store some properties, storage while we put this single attribute to NSData type Models cases in the NSUserDefaults, although after the app kill single case properties disappear in our NSUserDefaults will put this single cases of Models stored for the next start app when read single cases of Models in use.

Implementation of singleton:

  • MRC I do not say I would like to speak directly under the ARC single class implementation, the code.

Create a singleton class file (UserInfoContext) NSObject UserInfoContext.h file code inheritance: a new single Models / import singleton Modesl header file #import “UserInfo.h” @property (nonatomic, strong) UserInfo + (UserInfoContext *) *userInfo; sharedUserInfoContext; UserInfoContext.m file code: @synthesize userInfo; static UserInfoContext *sharedUserInfoContext = nil; (UserInfoContext*) sharedUserInfoContext{static + dispatch_once_t token; dispatch_once (& token, if ^{(sharedUserInfoContext = = Nil) {sharedUserInfoContext = [[self alloc] init]; return; sharedUserInfoContext;}})} + (ID) allocWithZone: (struct * _NSZone) zone{static dispatch_once_t token; dispatch_once (& token, if (sharedUserInfo ^{ Context = = Nil) {sharedUserInfoContext}}) = [super allocWithZone:zone]; return; sharedUserInfoContext;} – (instancetype) init{self = [super init]; if (self) {/ / sharedUserInfoContext.userInfo = [[UserInfo alloc] Models to instantiate the init] return self;};} – (ID) copy{return self;} – (ID) mutableCopy{return self a new single;} Models (UserInfo) NSObject UserInfo.h file storage property inheritance code: @interface UserInfo: NSObject< NSCoding> BOOL {isLogin; / / *phoneNumber / / NSString to determine whether the login mobile phone number; NSString *cookie; //cookie NSInteger tabBarSelected; //tabbar NSString *deviceid; / / @property} device number (nonatomic, assign) BOOL isLogin @pro; Perty (nonatomic, copy) NSString *phoneNumber @property (nonatomic, copy); NSString *cookie; @property (nonatomic, assign) NSInteger tabBarSelected @property (nonatomic, copy); NSString *deviceid; UserInfo.m file code (attribute encoding and decoding):@synthesize isLogin; @synthesize phoneNumber; @synthesize cookie; @synthesize tabBarSelected; @synthesize deviceid; – (void) encodeWithCoder: (NSCoder *) aCoder{[aCoder encodeBool:self.isLogin forKey:@ “isLogin”]; [aCoder encodeObject:self.phoneNumber forKey:@; [aCoder encodeObject:self.cookie “phoneNumber” forKey:@ “cookie”]; [aCoder encodeInteger:self.tabBarSelected forKey:@; [aCoder encodeObject:self.deviceid “tabBarSelected” forKey:@ “deviceid”];} – (instancetype) initWithCo Der: (NSCoder *) aDecoder{if (self =[super init]) {self.isLogin [aDecoder decodeBoolForKey:@ = “isLogin”]; self.phoneNumber [aDecoder decodeObjectForKey:@ = “phoneNumber”]; self.cookie [aDecoder decodeObjectForKey:@ = “cookie”]; self.tabBarSelected [aDecoder decodeIntegerForKey:@ = “tabBarSelected”]; self.deviceid [aDecoder decodeObjectForKey:@ = “deviceid”]}}; return self; @end a new type of tool (Utilities) for inherited attributes of NSObject in order to better achieve the simple using the singleton and NSUserDefaults read and write Utilities.h file code: / / single case -> NSUserDefaults + (void) SetNSUserDefaults: (UserInfo * userInfo); //NSUserDefaults-> single case + (UserInfo *) GetNSUserDefault S; Utilities.m file code: / / single storage cases of Models (UserInfo) to NSUserDefaults + (void) SetNSUserDefaults: (UserInfo *) userInfo{NSData *data = [NSKeyedArchiver archivedDataWithRootObject:userInfo]; NSUserDefaults *defaults = NSUserDefaults standardUserDefaults] [defaults setObject:data forKey:@ “;” user “]; [defaults synchronize];} / / read NSUserDefaults content stored in return to single Modesl (UserInfo) + (UserInfo *) GetNSUserDefaults{NSUserDefaults *defaults standardUserDefaults] NSData [NSUserDefaults = *data = [defaults; objectForKey:@” user “]; return [NSKeyedUnarchiver unarchiveObjectWithData:data];} the code we often start 1 we implement a singleton class (UserInfoContext);
2 cases in a single class In the implementation of a property of Models (UserInfo);
3 to achieve a convenient tool to single attribute Models (UserInfo) NSUserDefaults and the NSUserDefaults store to read the contents of single attribute Models (UserInfo) in patients with.

  • Below we call the test assignment to a single instance of Models (UserInfo) while the single instance Models (UserInfo) stored in NSUserDefaults

In the case of single storage / / *user = UserInfo [UserInfoContext sharedUserInfoContext].userInfo; user.isLogin = YES; user.phoneNumber = @ 13552539636 @ = “user.cookie” QQ396368888 “; / / TabBar user.tabBarSelected start app default = 0; / / in the single instance storage to NSUserDefaults (call tool method) [Utilities SetNSUserDefaults:user];

  • AppDelegate class in the didFinishLaunchingWithOptions method to read the contents of the NSUserDefaults directly to the use of a single case

/ / read the user status and configuration information to a single [UserInfoContext sharedUserInfoContext].userInfo= [Utilities GetNSUserDefaults] case;

  • Above we complete a process well, why should I do this, because we are a wonderful requirement is to start the default display which TabBar and some user settings like App themes and so on a series of requirements, so there are not enough cases.
  • The code is not on the GitHub this code is not less than a copy can be.