iOS XMPP 在对话屏幕中收到消息 [英] iOS XMPP received message in conversation screen

查看:27
本文介绍了iOS XMPP 在对话屏幕中收到消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究基于 XMPP 的项目.我可以发送消息并显示在对话屏幕中,但是当我收到消息时,它只显示在警报视图中,而无法在对话屏幕中看到.

I am working on XMPP based project. I am able to send message and it displays in conversation screen but when I receive message it only show in alertview not able to see in conversation screen.

 - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(messageReceived:) name:MessageRecivedNotif object:nil];

appdelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
[self getAllMessagesArrayWithOppositeUser:_jid];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWasShown:)
                                             name:UIKeyboardDidShowNotification
                                           object:nil];


}

#pragma mark table delegate and datasource methods
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tblobj dequeueReusableCellWithIdentifier:@"cell"];
UILabel *lbl;
if (cell==nil)
{
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

    if (chatLblRight == 1)
    {
        lbl = [[UILabel alloc]initWithFrame:CGRectMake(200, 10, 150, 44)];
        [lbl setTextColor:[UIColor redColor]];
        [cell.contentView   addSubview:lbl];
    }


 }
 if (chatLblRight == 1)
 {
    lbl.text = [[data objectAtIndex:indexPath.row]valueForKey:@"text"];
    chatLblRight = 0;
    lbl = [[UILabel alloc]initWithFrame:CGRectMake(200, 10, 150, 44)];
    [lbl setTextColor:[UIColor redColor]];
    [cell.contentView   addSubview:lbl];

    return cell;
  }
  else
  {
    cell.textLabel.text=[[data objectAtIndex:indexPath.row]valueForKey:@"text"];
    return cell;
  }
}
-(void)messageReceived:(NSNotification*)notif
{
XMPPMessage *message=(XMPPMessage*)notif.object;
NSString *body = [[message elementForName:@"body"] stringValue];
NSMutableDictionary *dic_recive = [[NSMutableDictionary alloc]init];
[dic_recive setObject:body forKey:@"text"];
[data addObject:dic_recive];
chatLblRight = 1;
[tblobj reloadData];
}
 -(IBAction)btnsend:(id)sender
{
[txtmsg resignFirstResponder];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];
[msgview setFrame:CGRectMake(0, self.view.bounds.size.height-53, self.view.bounds.size.width, 53)];
[UIView commitAnimations];
[((AppDelegate*)[[UIApplication sharedApplication]delegate]) sendMessage:txtmsg.text toUserWithJid:_jid];
NSMutableDictionary *dic_send = [[NSMutableDictionary alloc]init];
[dic_send setObject:txtmsg.text forKey:@"text"];
[data addObject:dic_send];
[tblobj reloadData];
txtmsg.text=@"";
 }
-(NSMutableArray *)getAllMessagesArrayWithOppositeUser:(XMPPJID *)xmppUser
{
XMPPMessageArchivingCoreDataStorage *storage = [XMPPMessageArchivingCoreDataStorage sharedInstance];
NSManagedObjectContext *moc = [storage mainThreadManagedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"XMPPMessageArchiving_Message_CoreDataObject"
                                                     inManagedObjectContext:moc];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
[request setEntity:entityDescription];
NSError *error;
NSArray *messages = [moc executeFetchRequest:request error:&error];

data=[[NSMutableArray alloc] init];

@autoreleasepool {
    for (XMPPMessageArchiving_Message_CoreDataObject *message in messages) {

        if ([message.bareJid isEqual:xmppUser])
        {
            NSMutableDictionary *dict=[[NSMutableDictionary alloc] init];
            [dict setObject:message.body forKey:@"text"];
            [dict setObject:message.timestamp forKey:@"date"];
            if ([message isOutgoing]) {
                [dict setObject:@"1" forKey:@"type"];
                [dict setObject:[[message.bareJidStr componentsSeparatedByString:@"@"] firstObject] forKey:@"username"];
                [dict setObject:[UIColor greenColor] forKey:@"color"];
            }
            else{

                [dict setObject:@"2" forKey:@"type"];
                [dict setObject:[[message.bareJidStr componentsSeparatedByString:@"@"] firstObject] forKey:@"username"];
                //                    [dict setObject:userName forKey:@"username"];
                [dict setObject:[UIColor blueColor] forKey:@"color"];
             }
            [data addObject:dict];
          }
      }
  }

   return data;
}

我的错误在哪里?

编辑

 - (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{

DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);


if ([message isChatMessageWithBody])
{
    XMPPMessageArchivingCoreDataStorage *xmppMessageStorage = [XMPPMessageArchivingCoreDataStorage sharedInstance];
    XMPPMessageArchiving *xmppMessageArchiving = [[XMPPMessageArchiving alloc] initWithMessageArchivingStorage:xmppMessageStorage];

    [xmppMessageArchiving activate:xmppStream];
    [xmppMessageArchiving addDelegate:self delegateQueue:dispatch_get_main_queue()];
    XMPPUserCoreDataStorageObject *user = [xmppRosterStorage userForJID:[message from]
                                                             xmppStream:xmppStream
                                                   managedObjectContext:[self managedObjectContext_roster]];

    [[NSNotificationCenter defaultCenter] postNotificationName:MessageRecivedNotif object:message];

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

    if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive)
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:displayName
                                                            message:body
                                                           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,body];

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

推荐答案

你需要像这样在 uitableviewcellforrowatindexpath 方法中设置你的逻辑.

you need to set your logic in uitableviewcellforrowatindexpath method like this.

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tblobj dequeueReusableCellWithIdentifier:@"cell"];
UILabel *lbl;
if (cell==nil)
{
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

    if([[[data objectAtIndex:indexPath.row]valueForKey:@"type"] isEqualToString:@"2"])
    //if (chatLblRight == 1)
    {
        lbl = [[UILabel alloc]initWithFrame:CGRectMake(200, 10, 150, 44)];
        [lbl setTextColor:[UIColor redColor]];
        [cell.contentView   addSubview:lbl];
    }


}
if([[[data objectAtIndex:indexPath.row]valueForKey:@"type"] isEqualToString:@"2"])

//if([[[data objectAtIndex:indexPath.row]valueForKey:@"type"] isEqualToString:@"1"])
//if (chatLblRight == 1)
{
    lbl.text = [[data objectAtIndex:indexPath.row]valueForKey:@"text"];
    chatLblRight = 0;
    lbl = [[UILabel alloc]initWithFrame:CGRectMake(200, 10, 150, 44)];
    [lbl setTextColor:[UIColor redColor]];
    [cell.contentView   addSubview:lbl];

    return cell;
}
else
{
    cell.textLabel.text=[[data objectAtIndex:indexPath.row]valueForKey:@"text"];
    return cell;
  }

}

请检查此代码一次.

这篇关于iOS XMPP 在对话屏幕中收到消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆