昨天完成了聊天界面,基本功能算告一段落

开发时间:五天(工作时间)

开发工具:xcode6

开发平台:iOS8

XMPP框架:XMPPFramework

git clone https://github.com/robbiehanson/XMPPFramework.git

界面设计:使用StoryBoard

github地址:https://github.com/hjandyz/XMPP

1.关于socket在后台的运行,iOS8可以直接使用(但是我自由在模拟器成功,真机不知为何不可以),ios7的话需要配置

其实很方便,初始化xmppstream以后

_XMPPStream.enableBackgroundingOnSocket = YES;

然后修改plist文件

2.获得后台运行的权限以后我们就可以接收到message了

#pragma mark 接收到好友消息

- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message

{

//判断是否在前台

if([UIApplication sharedApplication].applicationState != UIApplicationStateActive){

HJLog(@"后台");

UILocalNotification *localNoti = [[UILocalNotification alloc] init];

localNoti.alertBody = [NSString stringWithFormat:@"%@:%@",message.fromStr,message.body];

localNoti.soundName = @"default";

[[UIApplication sharedApplication] scheduleLocalNotification:localNoti];

}

}

当然iOS8要注册一下通知,用户允许才能发通知给用户

//注册应用接受通知

if([[UIDevice currentDevice].systemVersion doubleValue] > 8.0){

UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil];

[application registerUserNotificationSettings:setting];

}

3.发消息的时可以自己在xml文件中加入body,这样子可以区分图片声音还是文字或者其他格式

- (void)shendMessage:(NSString *)text bodyType:(NSString *)bodyType

{

XMPPMessage *message = [XMPPMessage messageWithType:@"chat" to:self.friendJid];

[message addAttributeWithName:@"chatType" stringValue:bodyType];

[message addBody:text];

[[HJXMPPTool sharedHJXMPPTool].XMPPStream sendElement:message];

}

4.一些小方法,为了显示新的消息自动混动到底部,用户拖动scrollView时候隐藏键盘

#pragma mark - 滚动到底部

- (void)scrollToTableViewBotton

{

NSInteger lastRow = _resultController.fetchedObjects.count - 1;

if (lastRow < 0) {

return;

}

NSIndexPath *path = [NSIndexPath indexPathForRow:lastRow inSection:0];

[self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionBottom animated:YES];

}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

{

[self.view endEditing:YES];

}

3.通过的到contentSize来让输入框最多变高成三行

#pragma mark - textView delegate

- (void)textViewDidChange:(UITextView *)textView

{

CGFloat contentH = textView.contentSize.height;

//    NSLog(@"%f",size.height);

if (contentH > 33 && contentH < 67) {

self.inputViewHeightConstraint.constant = contentH + 18;

}

NSString *text = textView.text;

if([text rangeOfString:@"\n"].length != 0){

//去除换行字符

text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

[self shendMessage:text bodyType:@"text"];

textView.text = nil;

self.inputViewHeightConstraint.constant = 50;

}

}

4.用代码实现自动布局(VLF)也是很有用的

#warning 代码实现自动布局,要设置下面为NO

tableView.translatesAutoresizingMaskIntoConstraints = NO;

//自动布局 VFL

NSDictionary *views = @{@"tableView":tableView, @"inputView":inputView};

NSArray *tableHConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[tableView]-0-|" options:0 metrics:nil views:views];

[self.view addConstraints:tableHConstraints];

NSArray *inputHConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[inputView]-0-|" options:0 metrics:nil views:views];

[self.view addConstraints:inputHConstraints];

NSArray *vConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[tableView]-0-[inputView(50)]-0-|" options:0 metrics:nil views:views];

[self.view addConstraints:vConstraints];

@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);

最新文章

  1. Centos——安装JDK
  2. 关于SSMS显示select出来的数据行的疑问
  3. php 简易购物习题
  4. Loggly:提高ElasticSearch性能的九个高级配置技巧
  5. Android中通过访问本地相册或者相机设置用户头像
  6. jacascript 函数参数与 arguments 对象
  7. re模块的方法总结
  8. pycharm配置运行django项目步骤
  9. Centos 安装 android sdk(转)
  10. 递归求i^2的和
  11. Oracle FM FM09999999 确保8位数字 即使全是0
  12. TensorFlow MNIST 问题解决
  13. Flink - InputGate
  14. 团队项目开发特点以及NABCD分析总结
  15. jQuery插件开发全解析[转]
  16. 团队作业——Alpha冲刺 12/12
  17. sql 跨服务器查询数据
  18. IDEA配置GIT
  19. Day21-模板之继承
  20. idea前端页面不刷新----springboot

热门文章

  1. Android原理View、ViewGroup
  2. Android导航菜单横向左右滑动并和下方的控件实现联动
  3. new SqlSessionFactoryBuilder().build(inputStream, properties)
  4. 【转】Android 4.0.3 CTS 测试
  5. java链接sqlite资料整理
  6. PHP批量审核后台
  7. Tcp实现简单的大小写转换功能
  8. Oracle sga、pga介绍改动
  9. (HYSBZ)BZOJ 1588 营业额统计
  10. ubuntu下30天自制操作系统还在继续学习中