The user's personal information will distinguish different permissions enjoyed by the user during the use of the APP. For example, when the user tries to click into a "Favorites" interface, it is necessary to judge the user's login status. If the user is in the login state, he can directly enter the "Favorites" interface. If the user is in the tourist state (that is, not logged in state), we cannot let the user directly enter the favorites interface. At this time, we can guide the user to log in by jumping to the login interface, or pop up a friendly reminder that the user is not logged in. Similar to login, when the user changes the avatar or nickname, when the modification is successful, the user's avatar or nickname will be updated in the background database. At this point, we also need to update the user's information locally to avoid the problem of mismatching the latest data when using the user's information somewhere. So the question comes, frequently used in the APP and update the user information, we need to create a single public class to unified management of user information, the user information to add, update, and delete all through the personal information management tools to deal with, so the user information processing becomes concise, easier to maintain and use.Copy the code
The above material is taken from the Internet, if there is infringement, please contact private deletion
All right, let’s cut to the chase. Let’s start by creating a simple utility class: UserInfoManager
1. The attributes of the UserInfo tool are all user information, including the most commonly used attributes, such as userID, nickname, and profile picture. The specific attributes can be determined based on the service requirements of your project.
2. Use the singleton mode to share the user information globally
3. (1) When we log in successfully or call the interface to obtain user information successfully, the server will return us the relevant information of the user. (Since there is no interface to show here, we will simulate the relevant data information by ourselves.)
NSDictionary *infoDic = @ {@ “userName” : @ “cat eggs,” @ “userID” : @ “akjhfakhewawehfakehffcnak,” @ “nickName:” @ “to thrive,” @ “headPic:” @ “www.baidu.com/pic”, “@” phon…” : @ 1};
At this point, we can pass the user information directly to the UserInfoManager utility class, which will help you configure the information into the user property list. Key is the attribute name and value is the value of the attribute.
[UserInfoManager configInfo:infoDic];
(2) This method takes out all key values in the dictionary, constructs setter methods of different attributes according to these key values, and calls the constructed setter methods.
Question: How does a setter method that calls a property store the value of the property in the user preference list? !
If you are not familiar with the Runtime, you can learn and communicate with each other. This article will not focus on the knowledge related to Runtime, but will simply explain the implementation of the functionality that appears in the utility class.
Replace setter methods for all attributes in the +load{} method with our own setMyAttribute:; In this case, when we set the property of the utility class, the setter method of the corresponding property will be called. Because we have replaced the original setter method with the custom setMyAttribute: method, the setMyAttribute method will be fired. In this method, we get the name of the original method (via _cmd), we parse the string from the name of the original setter method to find the property, and we have everything, the name of the property, and the value of the property (the parameter is the value of the property). By key-value, the information is stored in the local user preference Settings. Ok, so easy to fix ~~~~
(4) Of course, when fetching data, it also takes out data stored locally by means of exchanging methods; I won’t bore you here.
Writing is not good, may not be very clear about, I hope you give more advice. Do not forget to import <objc/runtime.h> file ~~~~
If you are interested in runtime, please leave your contact information.
Attach the code’s Github address: github.com/tandepeng/U…