[XMPP]iOS聊天软件学习笔记[四]

时间:2024-12-18 10:05:31

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

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

开发工具: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文件

[XMPP]iOS聊天软件学习笔记[四]

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);