[XMPP]iOS聊天软件学习笔记[三]
今天做了好友界面,其实xmpp内部已经写好很多扩展模块,所以使用起来还是很方便的
开发时间:五天(工作时间)
开发工具:xcode6
开发平台:iOS8
XMPP框架:XMPPFramework
git clone https://github.com/robbiehanson/XMPPFramework.git
界面设计:使用StoryBoard
github地址:https://github.com/hjandyz/XMPP
1.每一个模块创建以后都需要激活,比如自动连接模块
//自动连接模块
_reconnect = [[XMPPReconnect alloc] init];
[_reconnect activate:_XMPPStream];
2.CoreData 的用法
- (void)loadFriends
{
//1.上下文关联数据库
NSManagedObjectContext *context = [HJXMPPTool sharedHJXMPPTool].rosterSrorage.mainThreadManagedObjectContext;
//2.创建FetchRequest请求
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"XMPPUserCoreDataStorageObject"];
//3.过滤和排序
//当年登陆用户的好友
NSString *jid = [HJUserInfo sharedHJUserInfo].jid;
NSPredicate *pre = [NSPredicate predicateWithFormat:@"streamBareJidStr = %@",jid];
fetchRequest.predicate = pre;
//排序
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"displayName" ascending:YES];
fetchRequest.sortDescriptors = @[sort];
//4.执行请求
_resultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:nil cacheName:nil];
_resultsController.delegate = self;
NSError *err = nil;
[_resultsController performFetch:&err];
HJLog(@"%@",err);
}
3.销毁的方法也很重要
#pragma mark 销毁
- (void)teardownXmpp
{
//停止模块
[_XMPPStream removeDelegate:self];
[_reconnect deactivate];
[_avatar deactivate];
[_vCard deactivate];
[_roster deactivate];
//断块连接
[_XMPPStream disconnect];
//清空资源
_reconnect = nil;
_vCard = nil;
_vCardStorage = nil;
_avatar = nil;
_XMPPStream = nil;
_roster = nil;
_rosterSrorage = nil;
}
4.添加好友的方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSString *user = textField.text;
if (![textField isTelphoneNum]) {
[self showAlert:@"请输入正确的手机号码"];
return YES;
}
//判断是否自己或者好友已经存在
if ([user isEqualToString:[HJUserInfo sharedHJUserInfo].user]) {
[self showAlert:@"不能添加自己"];
}
XMPPJID *friendJid = [XMPPJID jidWithString:[NSString stringWithFormat:@"%@%@", user, DomainKey]];
if([[HJXMPPTool sharedHJXMPPTool].rosterSrorage userExistsWithJID:friendJid xmppStream:[HJXMPPTool sharedHJXMPPTool].XMPPStream]){
[self showAlert:@"已经是你的好友了"];
}
[[HJXMPPTool sharedHJXMPPTool].roster subscribePresenceToUser:friendJid];
return YES;
}
5.删除好友的方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
XMPPUserCoreDataStorageObject *user = _resultsController.fetchedObjects[indexPath.row];
XMPPJID *jid = user.jid;
[[HJXMPPTool sharedHJXMPPTool].roster removeUser:jid];
}
}
剩下的就不一一讲了,具体可以看代码
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。