你的位置:首页 > 软件开发 > 操作系统 > iOS开发之聊天模块

iOS开发之聊天模块

发布时间:2016-08-17 23:00:06
需求详解:  在实际开发中,有可能是在后期优化的时候,会有这么需要优化的需求:聊天输入框保存之前输入的文本,提高用户的良好体验。   在聊天模块中,用户可能会在输入框输入若干字符,但是没有点击发送就点击退出聊天,或者要点击用户头像确认用户的信息,或者比如需要向好友发送另一个好 友 ...

iOS开发之聊天模块

需求详解:

  在实际开发中,有可能是在后期优化的时候,会有这么需要优化的需求:聊天输入框保存之前输入的文本,提高用户的良好体验。

   在聊天模块中,用户可能会在输入框输入若干字符,但是没有点击发送就点击退出聊天,或者要点击用户头像确认用户的信息,或者比如需要向好友发送另一个好 友的ID不得不暂时退出当前好友聊天界面跳转找到别的界面找ID,然而当前聊天输入框也已经输入好了若干字符,用户当然不希望退出之后就删除之前输入好的 文字。所以这里就需要暂时保存用户输入好的但是没有发送出去的字符串。

  但是,还需要满足1、完全杀掉或者完全退出应用就需要清楚这个暂时保存的字符串,2、发送出去之后,肯定就要delegate之前暂时保存的字符串喽。

 

开始:

这部分逻辑的实现一开始我没怎么比较好的头绪,只想到本地序列化,但实际上这个还不算是最好的思路,因为本地序列化用到这里有点小题大做了,其实只要用全局静态变量的字典就可以了。

具体实现的逻辑,我也特意阅读研究了Coding项目的实现,毕竟这个项目是比较成熟的项目,聊天模块也做的很不错,所以学学别人的思想,正所谓站在巨人的肩膀上,也是很好的哦。

 那么下面,我就直接解读Coding源码(学习Coding-iOS开源项目日志(一))在这个聊天模块内容保存的逻辑吧,就不拿自己工作开发的项目来讲了。

1、首先声明全局static的变量,Coding中用inputStrDict存储输入框的字符串,而inputMediaDict我暂时不知道它具体存什么的,应该是media之类的元素:

 iOS开发之聊天模块

2、然后将很多逻辑封装在这个UIMessageInputView类中,方法都不用公开,完全利用UIMessageInputView活动周期的逻辑就可以了。

 

 1 #pragma mark remember input 2  3 - (NSMutableDictionary *)shareInputStrDict{ 4   if (!_inputStrDict) { 5     _inputStrDict = [[NSMutableDictionary alloc] init]; 6   } 7   return _inputStrDict; 8 } 9  10 - (NSMutableDictionary *)shareInputMediaDict{ 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 UIMessageInputViewContentTypeTweet: 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 shareInputStrDict] objectForKey:inputKey]); 45     return [[self shareInputStrDict] objectForKey:inputKey]; 46   } 47   return nil; 48 } 49  50 - (void)deleteInputData{ 51   NSString *inputKey = [self inputKey]; 52   DebugLog(@"inputKey_delegate:%@",inputKey); 53   if (inputKey) { 54     [[self shareInputStrDict] removeObjectForKey:inputKey]; 55     [[self shareInputMediaDict] 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 && inputKey.length > 0) { 64     if (inputStr && inputStr.length > 0) { 65       [[self shareInputStrDict] setObject:inputStr forKey:inputKey]; 66     }else{ 67       [[self shareInputStrDict] 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 shareInputMediaDict] setObject:_mediaList forKey:inputKey]; 77     }else{ 78       [[self shareInputMediaDict] removeObjectForKey:inputKey]; 79     } 80   } 81 } 82 83 - (NSMutableArray *)inputMedia{ 84   NSString *inputKey = [self inputKey]; 85   if (inputKey) { 86     return [[self shareInputMediaDict] 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:@"回复 %@", _toUser.name]: @"撰写评论"; 97     }else{ 98       self.placeHolder = @"请输入私信内容"; 99     }100     _inputTextView.selectedRange = NSMakeRange(0, _inputTextView.text.length);101     [_inputTextView insertText:inputStr? inputStr: @""];102     103     _mediaList = [self inputMedia];104     [self mediaListChenged];105   }106 }

原标题:iOS开发之聊天模块

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。