苹果开发者的福利--XMPP增加删除好友
在现阶段的通信服务中,各种标准都有,因此会出现无法实现相互连通,而 XMPP(Extensible Message and presence Protocol) 协议的出现,实现了整个及时通信服务协议的互通。有了这个协议之后,使用任何一个组织或者个人提供的即使通信服务,都能够无障碍的与其他的及时通信服务的用户进行交流。例如 google 公司 2005 年推出的 Google talk 就是一款基于 XMPP 协议的即时通信软件。下面我们就谈论一下如何简单的使用 XMPP 的好友添加
1 、在 XMPPFramework.h 中将需要用到的头文件打开
2、在storyboard创建好友列表界面和增加好友界面
创建和增加的时候应该注意两者之间的连线:
3 、定义查询结果存储器并且进行初始化
NSFetchedResultsController * fetch;
AppDelegate * delegate=[UIApplication sharedApplication].delegate;
//获取上下文
NSManagedObjectContext * context=[delegate.rosterStorage mainThreadManagedObjectContext];
//获取请求NSFetchRequest
NSFetchRequest * request=[NSFetchRequest fetchRequestWithEntityName:@"XMPPUserCoreDataStorageObject"];
//增加排序字段
NSSortDescriptor * des=[NSSortDescriptor sortDescriptorWithKey:@"sectionNum" ascending:YES];
[request setSortDescriptors:@[des]];
//对fetch进行初始化
fetch=[[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:context sectionNameKeyPath:@"sectionNum" ***Name:nil];
//设置***
[fetch setDelegate:self];
//开始查询
[fetch performFetch:nil];
NSFetchedResultsController * fetch;
AppDelegate * delegate=[UIApplication sharedApplication].delegate;
//获取上下文
NSManagedObjectContext * context=[delegate.rosterStorage mainThreadManagedObjectContext];
//获取请求NSFetchRequest
NSFetchRequest * request=[NSFetchRequest fetchRequestWithEntityName:@"XMPPUserCoreDataStorageObject"];
//增加排序字段
NSSortDescriptor * des=[NSSortDescriptor sortDescriptorWithKey:@"sectionNum" ascending:YES];
[request setSortDescriptors:@[des]];
//对fetch进行初始化
fetch=[[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:context sectionNameKeyPath:@"sectionNum" ***Name:nil];
//设置***
[fetch setDelegate:self];
//开始查询
[fetch performFetch:nil];
4 、创实现 tableView 的***方法
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return fetch.sections.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id<NSFetchedResultsSectionInfo> sections=fetch.sections[section];
return [sections numberOfObjects] ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tabble" ];
XMPPUserCoreDataStorageObject * user=[fetch objectAtIndexPath:indexPath];
cell.textLabel.text=user.displayName;
return cell;
}
//返回分组数据信息,根据字段type来控制当前的状态
-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
id<NSFetchedResultsSectionInfo> info= [[fetch sections] objectAtIndex:section];
NSString * type=nil;
if ([info.name isEqualToString:@"0"]) {
type=@"在线";
}else {
type=@"离线";
}
return type;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return fetch.sections.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id<NSFetchedResultsSectionInfo> sections=fetch.sections[section];
return [sections numberOfObjects] ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tabble" ];
XMPPUserCoreDataStorageObject * user=[fetch objectAtIndexPath:indexPath];
cell.textLabel.text=user.displayName;
return cell;
}
//返回分组数据信息,根据字段type来控制当前的状态
-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
id<NSFetchedResultsSectionInfo> info= [[fetch sections] objectAtIndex:section];
NSString * type=nil;
if ([info.name isEqualToString:@"0"]) {
type=@"在线";
}else {
type=@"离线";
}
return type;
}
5 、删除好友
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
//通过花名册对象对当前的好友进行删除操作-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle==UITableViewCellEditingStyleDelete) {AppDelegate * delegate=[UIApplication sharedApplication] .delegate;
XMPPUserCoreDataStorageObject * user=[fetch objectAtIndexPath:indexPath];
[delegate.roster removeUser:user.jid];
}
}
6、增加好友
- (IBAction)add {//获取好友名称NSString * name=self.friends.text;//获取好友名称AppDelegate * delegate=[UIApplication sharedApplication].delegate;XMPPJID * jid=[XMPPJID jidWithString:name];if ([delegate.rosterStorage userExistsWithJID:jid xmppStream:delegate.stream]) {NSLog(@"好友已经存在了");}else{
//增加好友[delegate.roster subscribePresenceToUser:jid];
}
}