gpt4 book ai didi

iOS XMPP 在对话屏幕中收到消息

转载 作者:行者123 更新时间:2023-11-29 10:28:40 25 4
gpt4 key购买 nike

我正在从事基于 XMPP 的项目。我能够发送消息并显示在对话屏幕中,但是当我收到消息时,它仅显示在警报 View 中,无法在对话屏幕中看到。

 - (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 方法中设置您的逻辑。

 - (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 在对话屏幕中收到消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30844763/

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