XMPP 发送消息,发送图片

时间:2023-01-28 19:55:58

在 AppDelegate.h类中添加两个属性

@property (strong,nonatomic) XMPPMessageArchiving * msgArchiving;
@property (strong,nonatomic) XMPPMessageArchivingCoreDataStorage * msgArchivingStorage;

在AppDelegate.m类中注册消息模块

- (void)connect{
// 5.添加消息模块
self.msgArchivingStorage = [XMPPMessageArchivingCoreDataStorage sharedInstance];
self.msgArchiving = [[XMPPMessageArchiving alloc] initWithMessageArchivingStorage:self.msgArchivingStorage];
[self.msgArchiving activate:self.xmppStream];
}

释放资源

- teardownStream{
[self.msgArchiving deactivate];
self.msgArchiving = nil;
self.msgArchivingStorage = nil;
}

聊天界面的消息

@interface MeTableViewController ()<NSFetchedResultsControllerDelegate>
// 消息的结果集
@property (strong,nonatomic) NSFetchedResultsController * resultsController;

@end

/**
* 获得消息的结果集
*/

- (void)loadChat
{
// 上下文
NSManagedObjectContext * msgContext = [[AppDelegate alloc] init].msgArchivingStorage.mainThreadManagedObjectContext;

// 执行查询
NSFetchRequest * request = [NSFetchRequest fetchRequestWithEntityName:@"XMPPMessageArchiving_Message_CoreDataObject"];

// 设置过滤
NSPredicate *pre = [NSPredicate predicateWithFormat:@"streamBareJidStr = %@ AND bareJidStr = %@",loginUserJid,friendJid.bare];
request.predicate = pre;

// 设置排序
NSSortDescriptor * timeSort = [NSSortDescriptor sortDescriptorWithKey:@"timestamp" ascending:YES];
request.sortDescriptors = @[timeSort];

// 执行请求
self.resultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:msgContext sectionNameKeyPath:nil cacheName:nil];
self.resultsController.delegate = self;

NSError * error = nil;
[self.resultsController performFetch:&error];
if (error) {
NSLog(@"%@",[[error userInfo] description]);
}

}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

// 结果集数组中的元素
return self.resultsController.fetchedObjects.count;
}

/**
* 获取消息的列表
*/

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * ID = @"cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];

if (cell==nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}

// 获去聊天消息对象
XMPPMessageArchiving_Message_CoreDataObject * msgObj = self.resultsController.fetchedObjects[indexPath.row];
// 消息在 body 中
cell.textLabel.text = msgObj.body;

return cell;
}

/**
* 发送消息
*/

- (void)sendmsg:(NSString*)msgText
{
XMPPMessage *msg = [XMPPMessage messageWithType:@"chat" to:self.friendJid];
// 消息在 body 中
[msg addBody:msgText];
// 发送到服务器
[[[AppDelegate alloc] init].xmppStream sendElement:msg];
}


/**
* 发送图片,将图片转化成NSData
*/

- (void)sendAttachmentWithData:(NSData*)data bodyType:(NSString*)bodyType
{
// 1.定义一个 msg
XMPPMessage * msg = [XMPPMessage messageWithType:@"chat" to:self.friendJid];

// 2.设置msg 类型
[msg addAttributeWithName:@"bodyType" stringValue:bodyType];

// 3.添加 msg bodyType
[msg addBody:bodyType];
// [msg addBody:@"sound"];
// [msg addBody:@"doc"];
// [msg addBody:@"xls"];

// 附件经过"base64编码"转成字符串
NSString * base64Str = [data base64EncodedStringWithOptions:0];

// 定义附件
XMPPElement *attachment = [XMPPElement elementWithName:@"attachment" stringValue:base64Str];

// 4.msg添加子节点
[msg addChild:attachment];

// 5.发送数据
[[[AppDelegate alloc] init].xmppStream sendElement:msg];

}