我設計使用羅比漢森的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定義?
保留對字典所在對象的引用,通過屬性公開字典,使用它。 – 2013-06-25 06:27:52