Skip to content

利用NSUserDefaults,Keychain,runtime和归档快速存储用户信息,简单粗暴,集成简单.

Notifications You must be signed in to change notification settings

wang82426107/SDUserDefaults

Repository files navigation

All attributes must follow the NSCoding protocol, including the elements in the array member variables and dictionary member variables. The custom class can inherit from SDCodingObject. The Foundation of the base class is implemented NSCoding protocol methods.

中文文档


Import mode


Manual import: Download the Demo and SDUserDefaults. Import the SDUserDefaults folder into the appropriate location for your own project.

Cocoapod import: The method is shown below.

pod 'SDUserDefaults'

next pod install


Basic usage (manual import is recommended)


1.Download and import SDUserDefaults.

2.Add the properties you want to store in the SDUserDefaults.h file. The important thing to note here is that the properties must be NSCoding compliant classes, which are already in Foundation.

At this point someone asks, what do I need to do with my custom class? Do I need to implement - (void)encodeWithCoder and - (instancetype)initWithCoder methods in the NSCoding protocol? Absolutely not! You need to inherit from the SDCodingObject class, where I've implemented the NSCoding protocol, and all the properties are archived, such as the TestModel class shown above. The code is shown below.

3.Save Data: we simply assign the corresponding property and call the saveUserInfoAction method. The code is shown below.

[SDUserDefaults standardUserDefaults].name = @"用户数据";
TextModel *testModel = [[TextModel alloc] init];
testModel.name = @"骚栋";
testModel.age = @(15);
testModel.location = @"北京";
[SDUserDefaults standardUserDefaults].testModel = testModel;
[[SDUserDefaults standardUserDefaults] saveAllPropertyAction]; // 存储数据

4.Get Data:The code is shown below.

/*****获取数据*****/
NSLog(@"%@",[SDUserDefaults standardUserDefaults].name);
NSLog(@"%@",[SDUserDefaults standardUserDefaults].testModel.name);
NSLog(@"%@",[SDUserDefaults standardUserDefaults].testModel.age);
NSLog(@"%@",[SDUserDefaults standardUserDefaults].testModel.location);

5.Delete All Data:The code is shown below.

[[SDUserDefaults standardUserDefaults] deleteAllPropertyAction];

6.Update Data:If you want to delete a property, set that property to nil; If you want to modify a property, modify that property and save it by calling saveAllPropertyAction.

[SDUserDefaults standardUserDefaults].name = @"新的用户数据";
[SDUserDefaults standardUserDefaults].testModel.location = nil;
[[SDUserDefaults standardUserDefaults] saveAllPropertyAction]; // 更新数据

7.Ignore Archive Attributes:What if you don't want some properties to be archived and persisted? Use unEncodePropertys. If you add an ignored attribute name, the attribute will not be archived. Note that you should ignore before using saveAllPropertyAction as far as possible.

[SDUserDefaults standardUserDefaults].testModel.unEncodePropertys = @[@"age",@"names"];

Custom usage (import mode recommended: manual import and cocoapods are also available)


A lot of people would say, why should I initialize with your SDUserDefaults, I just want to write my own UserDefaults singleton to manage my own storage properties, can't I?

OK, of course. In version 1.0.3, I added a new class called the SDUserObject class, which only needs the singleton you wrote to inherit from the class and initialize it with the -(instancetype) initWithIdentifier method. Then you can still use powerful properties, But the amount of code you write will be greatly reduced. Let's take a look at how the examples in Demo are used.

We need the singleton class to inherit from the SDUserObject class, as shown in the figure below.

Then, when creating the singleton, it needs to be initialized using the - (instancetype)initWithIdentifier method, as shown below.

Use saveAllPropertyAction and deleteAllPropertyAction on the save and delete methods.


SDUserDefaults stored in Keychain


In version 1.0.6, I have added Keychain storage. We can implement Keychain storage in two ways.

  • When manually importing SDUserDefaults, we can modify the SDKeychainUserDefaults, using the same methods as the SDUserDefaults class. However, the storage location of SDKeychainUserDefaults is in the Keychain, not in NSUserDefaults.

  • Cocoapods import way only need to use initKeychainObjectWithIdentifier:. As shown below. You can view the Demo of TestUserDefaults initialization method.


SDUserDefaults Blog


参与修订者:贾璐

About

利用NSUserDefaults,Keychain,runtime和归档快速存储用户信息,简单粗暴,集成简单.

Resources

Stars

Watchers

Forks

Packages

No packages published