2013-06-25 90 views
0

我设计使用罗比汉森的XMPP架构的iOS聊天应用:https://github.com/robbiehanson/XMPPFramework存储在的NSMutableDictionary接收XMPP消息

我能够存储对此我发送到字典中这是我的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]; 
     } 
    } 
} 

我怎么能存储消息到我里面ChatViewController.m消息字典,其中的sendMessage定义?

+0

保留对字典所在对象的引用,通过属性公开字典,使用它。 – 2013-06-25 06:27:52

回答

0

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

在AppDelegates方法的调用将是这样的:

[self.chatViewController addMessage:messageDictionary]; 
+0

我是新来的,我能看到一些代码来更好地理解它吗?特别是,如何从AppDelegate调用字典?我的消息正在AppDelegate收到,我试图从ChatViewController显示。 – Jyotiska

+0

我不能说如何在你的情况下完全做到这一点,因为我没有关于你的应用程序的布局的信息。如果ChatViewController恰好是您的Windows rootViewController,那么您可以简单地转换rootViewController并使用该方法。 – Karl

0

这将是更好地将它们存储在本地SQLite,让您可以在以后轻松检索旧邮件。

1

您可以激活一个名为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类。顺便说一下,如果您在Table View Controller中显示所有消息,则每次插入新对象时(即发送或接收到新消息),都可以使用NSFetchedResultController刷新该Table View。

+0

Hello @moral,如果是MUC,该怎么办?如何激活XMPPRoomCoreDataStorage? –