Chat module for iOS development-content storage logic implementation, ios Module
Detailed requirements:
In actual development, it is possible that there is a need for optimization in the later stage: the text entered before is saved in the chat input box to improve the user experience.
In the chat module, the user may enter several characters in the input box, but click to exit the chat without clicking send, or click the user's profile picture to confirm the user's information, for example, if you want to send another friend's ID to a friend, you have to temporarily exit the current friend's chat interface and jump to another interface to find the ID. However, the current chat input box has already entered several characters, of course, the user does not want to delete the entered text after exiting. Therefore, we need to temporarily Save the string that the user entered but not sent out.
However, if you want to completely kill or exit the application, you need to clear the temporarily saved string. 2. After sending the string, you must temporarily save it before delegate.
Start:
I didn't have a good idea at the beginning about the implementation of this part of logic. I only thought about local serialization, but in fact this is not the best idea, because local serialization is used here for a little bit of a problem, in fact, you only need to use the dictionary of Global static variables.
The specific implementation logic, I also specifically read and studied the implementation of the Coding project. After all, this project is a relatively mature project and the chat module is doing well, so I learned others' ideas, standing on the shoulders of giants is also good.
So next, I will directly explain the logic of Coding source code (learn about Coding-iOS open source project log (1) stored in the chat module content, you don't need to talk about your own development projects.
1. First, declare the Global static variable. inputStrDict is used in Coding to store the string of the input box. inputMediaDict does not know what it actually stores. It should be an element such as media:
2. Then, we encapsulate a lot of logic in this UIMessageInputView class, And do not disclose the methods. We can simply use the logic of the UIMessageInputView activity cycle.
1 # pragma mark remember input 2 3-(NSMutableDictionary *) using inputstrdict {4 if (! _ InputStrDict) {5 _ inputStrDict = [[NSMutableDictionary alloc] init]; 6} 7 return _ inputStrDict; 8} 9 10-(NSMutableDictionary *) using inputmediadict {11 if (! _ InputMediaDict) {12 _ inputMediaDict = [[NSMutableDictionary alloc] init]; 13} 14 return _ inputMediaDict; 15} 16 17-(NSString *) inputKey {18 NSString * inputKey = nil; 19 if (_ contentType = UIMessageInputViewContentTypePriMsg) {20 inputKey = [NSString stringWithFormat: @ "privateMessage _ % @", self. toUser. global_key]; 21} else {22 if (_ commentOfId) {23 switch (_ contentType) {24 case UIMessageIn PutViewContentTypeTweet: 25 inputKey = [NSString stringWithFormat: @ "tweet _ % @", _ commentOfId. stringValue, _ toUser. global_key.length> 0? _ ToUser. global_key: @ ""]; 26 break; 27 case UIMessageInputViewContentTypeTopic: 28 inputKey = [NSString stringWithFormat: @ "topic _ % @", _ commentOfId. stringValue, _ toUser. global_key.length> 0? _ ToUser. global_key: @ ""]; 29 break; 30 case UIMessageInputViewContentTypeTask: 31 inputKey = [NSString stringWithFormat: @ "task _ % @", _ commentOfId. stringValue, _ toUser. global_key.length> 0? _ ToUser. global_key: @ ""]; 32 break; 33 default: 34 break; 35} 36} 37} 38 return inputKey; 39} 40 41-(NSString *) inputStr {42 NSString * inputKey = [self inputKey]; 43 if (inputKey) {44 DebugLog (@ "inputStr_get: % @", [[self defined inputstrdict] objectForKey: inputKey]); 45 return [[self defined inputstrdict] objectForKey: inputKey]; 46} 47 return nil; 48} 49 50-(void) deleteInputData {51 NSString * in PutKey = [self inputKey]; 52 DebugLog (@ "inputKey_delegate: % @", inputKey); 53 if (inputKey) {54 [[self defined inputstrdict] removeObjectForKey: inputKey]; 55 [[self defined inputmediadict] removeObjectForKey: inputKey]; 56} 57} 58 59-(void) saveInputStr {60 NSString * inputStr = _ inputTextView. text; 61 NSString * inputKey = [self inputKey]; 62 DebugLog (@ "inputKey_save: % @", inputKey); 63 if (inputKey & in PutKey. length> 0) {64 if (inputStr & inputStr. length> 0) {65 [[self defined inputstrdict] setObject: inputStr forKey: inputKey]; 66} else {67 [[self defined inputstrdict] removeObjectForKey: inputKey]; 68} 69} 70} 71 72-(void) saveInputMedia {73 NSString * inputKey = [self inputKey]; 74 if (inputKey & inputKey. length> 0) {75 if (_ mediaList. count> 0) {76 [[self defined inputmediadict] setObject: _ m EdiaList forKey: inputKey]; 77} else {78 [[self defined inputmediadict] removeObjectForKey: inputKey]; 79} 80} 81} 82 83-(NSMutableArray *) inputMedia {84 NSString * inputKey = [self inputKey]; 85 if (inputKey) {86 return [[self defined inputmediadict] objectForKey: inputKey]; 87} 88 return nil; 89} 90 91-(void) setToUser :( User *) toUser {92 _ toUser = toUser; 93 NSString * inputStr = [self inputStr]; 94 if (_ inputTextView) {95 if (_ contentType! = UIMessageInputViewContentTypePriMsg) {96 self. placeHolder = _ toUser? [NSString stringWithFormat: @ "Reply % @", _ toUser. name]: @ "write comments"; 97} else {98 self. placeHolder = @ "enter private message content"; 99} 100 _ inputTextView. selectedRange = NSMakeRange (0, _ inputTextView. text. length); 101 [_ inputTextView insertText: inputStr? InputStr: @ ""]; 102 103 _ mediaList = [self inputMedia]; 104 [self mediaListChenged]; 105} 106}
The above is nothing more than splicing the chat object name into a key value, and then storing the string of the current input box to the Global static dictionary, then there are several methods to retrieve and delete.
3. Let's see where these methods are called:
The stored method is put in the frame rewriting method, because the input box will switch frame with the reality and hiding of the keyboard. However, my colleagues developed the chat module at the beginning of my project, I found that he used the layout code of Masonry to change the position of the input box. Selecting the layout constraint means that the frame is abandoned. Therefore, where to call the save method should be implemented based on actual needs and actual encoding. In addition, when developing this input box, you can consider its operation cycle: Start to edit-> end to edit, these operation cycles can implement their respective methods, just like the lifecycle of a controller. In short, there are a lot of ideas. Doing a good job can reflect the logic of managing and maintaining well.
Then look for the delete method. The delete method is placed at the top of the string, because the elements stored in the dictionary can be deleted as they have been sent out.
In addition, when creating a key, this key string depends on the current chat object, because the content of the current input box must match the current friend object one by one, I cannot save the content of the input box corresponding to the current friend, but the same content is displayed when I jump to another friend. Therefore, the key value must be determined based on the current friend's string. Therefore, the set Method of the ToUser attribute is rewritten in the Coding source code:
Respect the fruits of labor, reprinted with the source: http://www.cnblogs.com/goodboy-heyang/p/5782201.html