繁体   English   中英

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

[英]Storing received XMPP message in a NSMutableDictionary

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

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

- (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包含的所有类。 顺便说一句,如果您在表视图控制器中显示所有消息,则可以在每次插入新对象(即,已发送或接收新消息)时使用NSFetchedResultController刷新该表视图。

您可以为ViewController创建方法,例如- (void)addMessage:(NSDictionary *)messageProperties在其中将该消息添加到数组中并重新加载tableView。 如果您在AppDelegate中具有该ViewController的引用,则可以从那里调用它。

AppDelegates方法中的调用将如下所示:

[self.chatViewController addMessage:messageDictionary];

最好将它们存储在本地sqlite中,以便以后可以轻松检索旧邮件。

您可以通过这种方式通过协议管理信息

AppDelegate中

    protocol ChatDelegate {
    func MassageRecibed(name: String)
}

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate, UNUserNotificationCenterDelegate, XMPPStreamDelegate, XMPPRosterDelegate {


    var delegate:ChatDelegate! = nil
    func xmppStream(_ sender: XMPPStream, didReceive message: XMPPMessage) {
          delegate?.MassageRecibed(name: body)
       }

}

并在所需的视图中获取信息,您只需实现此协议,即实现Appdelegate的委托。

ChatViewController中

    class ChatViewController: UIViewController, ChatDelegate {

     let appDelegate = UIApplication.shared.delegate as! AppDelegate

      override func viewDidLoad() {
            super.viewDidLoad()

            appDelegate.delegate = self

        }

        func MassageRecibed(name: String) {
            print("mensaje recibido", name)

        }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM