实现步骤
// 花名册模块
#import "XMPPRoster.h"
#import "XMPPRosterCoreDataStorage.h"
// 花名册模块
@property (nonatomic, strong,readonly)XMPPRoster *roster;
// 花名册数据存储
@property (nonatomic, strong,readonly)XMPPRosterCoreDataStorage *rosterStorage;
- 3、修改AppDelegate.m文件中的setupXmppStream方法,添加花名册模块:
#pragma mark 初始化xmppStrem对象
-(void)setupXmppStream{
NSAssert(_xmppStream == nil, @"xmppStream对象初始化多次");
//1.创建xmppStrem对象
_xmppStream = [[XMPPStream alloc] init];
//2.添加代表
[_xmppStream addDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
//3.添加自动连接模块
_reconnect = [[XMPPReconnect alloc] init];
//激活
[_reconnect activate:_xmppStream];
// 电子名片数据存储
_vCardStorage = [XMPPvCardCoreDataStorage sharedInstance];
//4.添加电子名片模块
_vCardModule = [[XMPPvCardTempModule alloc] initWithvCardStorage:_vCardStorage];
//激活
[_vCardModule activate:_xmppStream];
//5.添加头像模块
_vCardAvatar = [[XMPPvCardAvatarModule alloc] initWithvCardTempModule:_vCardModule];
[_vCardAvatar activate:_xmppStream];
// 6. 添加花名册模块
_rosterStorage = [[XMPPRosterCoreDataStorage alloc] init];
_roster = [[XMPPRoster alloc] initWithRosterStorage:_rosterStorage];
[_roster activate:_xmppStream];
}
- 4、修改AppDelegate.m文件中,释放资源
#pragma mark 释放资源
-(void)teardownXmppstream{
// 移动代理
[_xmppStream removeDelegate:self];
//停止模块
//停止自动连接模块
[_reconnect deactivate];
// 停止电子名片模块
[_vCardModule deactivate];
// 停止头像模块
[_vCardAvatar deactivate];
// 停止花名册模块
[_roster deactivate];
//断开连接
[_xmppStream disconnect];
//清空资源为nil
_xmppStream = nil;
_reconnect = nil;
_vCardModule = nil;
_vCardStorage = nil;
_vCardAvatar = nil;
_roster = nil;
_rosterStorage = nil;
}
展示企业名册 到 JPRosterViewController控制器视图中
#import "JPRosterViewController.h"
#import "JPAppDelegate.h"
@interface JPRosterViewController () <NSFetchedResultsControllerDelegate>{
NSFetchedResultsController *_resultsContr;
}
@property (nonatomic, strong) NSArray *friends;
@end
@implementation JPRosterViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//从数据库获取好友列表
//1.拿上下文
NSManagedObjectContext *rosterContext = xmppDelegate.rosterStorage.mainThreadManagedObjectContext;
//2.创建查询对象
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"XMPPUserCoreDataStorageObject"];
//3.设置排序
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"displayName" ascending:YES];
request.sortDescriptors = @[sort];
//第一种方法查询数据
//4.执行请求
// NSError *error = nil;
// self.friends = [rosterContext executeFetchRequest:request error:&error];
// JPLogInfo(@"%@",self.friends);
// if (error) {
// JPLogInfo(@"%@",error);
// }
//
//第二种方法查询数据
// _resultsContr = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:rosterContext sectionNameKeyPath:nil cacheName:nil];
//sectionNum是在线状态 根据在线状态进行分组查询
_resultsContr = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:rosterContext sectionNameKeyPath:@"sectionNum" cacheName:nil];
//执行查询
NSError *error = nil;
[_resultsContr performFetch:&error];
if (error) {
NSLog(@"%@",error);
}
//设置代理
_resultsContr.delegate = self;
NSArray *mFriends = [_resultsContr fetchedObjects];
NSLog(@"%@=",mFriends);
}
@end
- 2、将数据展示到控制器的表格中,实现数据源方法,这里:按照好友的状态来进行分组实现,如下:
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
//分组数据
return _resultsContr.sections.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
//return [_resultsContr fetchedObjects].count;
//获取分组的信息
id<NSFetchedResultsSectionInfo> sectionInfo = _resultsContr.sections[section];
return [sectionInfo numberOfObjects];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//获取分组的信息
id<NSFetchedResultsSectionInfo> sectionInfo = _resultsContr.sections[section];
//indexTitle就是分组字段(sectionNum)的值
CZLogInfo(@"%@",[sectionInfo indexTitle]);
NSString *title = nil;
int state = [[sectionInfo indexTitle] intValue];
switch (state) {
case 0:
title = @"在线";
break;
case 1:
title = @"离开";
break;
case 2:
title = @"离线";
break;
default:
title = @"未知状态";
break;
}
return title;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"FriendCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// Configure the cell...
// XMPPUserCoreDataStorageObject *friend = self.friends[indexPath.row];
//获取对应的好友
//XMPPUserCoreDataStorageObject *friend = [_resultsContr fetchedObjects][indexPath.row];
//获取分组后的好友信息
XMPPUserCoreDataStorageObject *friend = [_resultsContr objectAtIndexPath:indexPath];
cell.textLabel.text = friend.displayName;
return cell;
}
删除好友功能的实现
- 1.我们可以遵守NSFetchedResultsControllerDelegate协议,让控制器成为 _resultsContr.delegate = self;然后实现协议的方法,就可以监听到好友数据的改变了
//删除好友
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//获取好友
XMPPUserCoreDataStorageObject *friend = [_resultsContr objectAtIndexPath:indexPath];
[xmppDelegate.roster removeUser:friend.jid];
}
}
#pragma mark NSFetchedResultsController的代理
/**
* 查询好友的数据改变会调用这个方法(比如,删除好友,添加好友)
*
* @param controller <#controller description#>
*/
-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller{
CZLogInfo(@"controllerDidChangeContent");
[self.tableView reloadData];
}
添加好友
#import "JPAddRosterViewController.h"
#import "JPAppDelegate.h"
@interface JPAddRosterViewController ()
- (IBAction)addFriend;
@property (weak, nonatomic) IBOutlet UITextField *jidField;
@end
@implementation JPAddRosterViewController
- (IBAction)addFriend {
//获取域名
NSString *domain = xmppDelegate.xmppStream.hostName;
NSString *jid = self.jidField.text;
JPLogInfo(@"添加好友 %@",jid);
//获取jid有没有域名(@teacher.local)
NSString *rangeStr = [NSString stringWithFormat:@"@%@",domain];
NSRange range = [jid rangeOfString:rangeStr];
//如果没有域名,自己添加完整的jid
if (range.location == NSNotFound) {
jid = [jid stringByAppendingString:rangeStr];
}
XMPPJID *friendJid = [XMPPJID jidWithString:jid];
//有可能好友已经存在,不须要添加
BOOL exists = [xmppDelegate.rosterStorage userExistsWithJID:friendJid xmppStream:xmppDelegate.xmppStream];
//用户已经存在
if (exists) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"当前添加的好友已经存在,无须添加" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
[alert show];
return;
}
//好友添加
JPLogInfo(@"%@",jid);
[xmppDelegate.roster subscribePresenceToUser:friendJid];
//销毁控制器
[self.navigationController popViewControllerAnimated:YES];
}
@end