gpt4 book ai didi

iphone - 将收到的 XMPP 消息存储在 NSMutableDictionary 中

转载 作者:行者123 更新时间:2023-11-29 03:49:09 24 4
gpt4 key购买 nike

我正在使用 Robbie Hanson 的 XMPP 框架设计一个 iOS 聊天应用程序:https://github.com/robbiehanson/XMPPFramework

我可以使用以下代码将发送的消息存储到字典中,该字典是我的表格 View 的数据源:

- (IBAction)sendMessage {

NSString *messageStr = messageField.text;
if([messageStr length] > 0) {
NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
[body setStringValue:messageStr];
NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
[message addAttributeWithName:@"type" stringValue:@"chat"];
[message addAttributeWithName:@"to" stringValue:chatWithUser];
[message addChild:body];
[[[self appDelegate] xmppStream] sendElement:message];

NSMutableDictionary *m = [[NSMutableDictionary alloc] init];
[m setObject:messageStr forKey:@"msg"];
[m setObject:@"you" forKey:@"sender"];
[messages addObject:m];
[self.tView reloadData];
}
}

但是 didReceiveMessage 是在 AppDelegate 内部定义的,我无法将收到的消息存储在本地字典中,因此无法在 TableView 中显示。我的 didReceiveMessage 函数如下所示:

- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);

// A simple example of inbound message handling.

if ([message isChatMessageWithBody])
{
XMPPUserCoreDataStorageObject *user = [xmppRosterStorage userForJID:[message from]
xmppStream:xmppStream
managedObjectContext:[self managedObjectContext_roster]];

NSString *messageBody = [[message elementForName:@"body"] stringValue];
NSString *displayName = [user jidStr];

if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive)
{

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:displayName
message:messageBody
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];

}
else
{
// We are not active, so use a local notification instead
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertAction = @"Ok";
localNotification.alertBody = [NSString stringWithFormat:@"From: %@\n\n%@",displayName,messageBody];

[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}
}
}

如何将消息存储到定义了 sendMessage 的 ChatViewController.m 内的消息字典中?

最佳答案

您可以激活名为 XMPPMessageArchiving 的模块。使用此模块,您可以保存所有传出和传入的消息(发送/接收的消息)。

XMPPMessageArchivingCoreDataStorage *xmppMessageStorage = [XMPPMessageArchivingCoreDataStorage sharedInstance];
XMPPMessageArchiving *xmppMessageArchiving = [[XMPPMessageArchiving alloc] initWithMessageArchivingStorage:xmppMessageStorage];

[xmppMessageArchiving activate:xmppStream];
[xmppMessageArchiving addDelegate:self delegateQueue:dispatch_get_main_queue()];

此扩展是 XEP 136 ( http://xmpp.org/extensions/xep-0136.html ),您可以使用 XMPPFramework 中包含的所有类。顺便说一句,如果您在 TableView Controller 中显示所有消息,则可以在每次插入新对象(即发送或接收新消息)时使用 NSFetchedResultController 刷新该 TableView 。

关于iphone - 将收到的 XMPP 消息存储在 NSMutableDictionary 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17290471/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com