0
votes

I am making an app based on chatting i am able to send the messages but unable to recieve messages in my chat screen so can any body help me out here is my code

if ([[message elementForName:@"body"] stringValue]==nil) {

}
else
{

    NSString *messageBody = [[message elementForName:@"body"] stringValue];
    XMPPUserCoreDataStorageObject *user = [xmppRosterStorage userForJID:[message from]
                                                             xmppStream:xmppStream
                                                   managedObjectContext:[self managedObjectContext_roster]];
    NSString *displayName = [user displayName];
    BOOL isComposing = NO;

    NSString *MyString;
    NSDate *now = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    // [dateFormatter setDateFormat:@"yyyy-MM-dd-HH-mm-ss"];
    [dateFormatter setDateFormat:@"HH:mm,yyyy/MM/dd"];
    MyString = [dateFormatter stringFromDate:now];
    NSLog(@"TIME AND DATE=>%@",MyString);

    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    NSString *myJID=[NSString stringWithFormat:@"%@@xxxx-mac-mini-2.local",[defaults valueForKey:@"userNameJID"]];
    NSString *friendID=[[[message attributeForName:@"from"] stringValue] stringByDeletingLastPathComponent];
      NSLog(@"---chatToUserId=>%@",friendID);




     if ([message isChatMessageWithBody]){

        XMPPUserCoreDataStorageObject *user = [xmppRosterStorage userForJID:[message from]
                                                                 xmppStream:xmppStream
                                                       managedObjectContext:[self managedObjectContext_roster]];

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

        //  [xmppMessageArchivingStorage archiveMessage:message outgoing:NO  xmppStream:xmppStream];



        NSMutableDictionary *m = [[NSMutableDictionary alloc] init];
        [m setObject:msg forKey:@"msg"];
        [m setObject:from forKey:@"sender"];}

in xmpp delegate method that is didReceiveMessage but how can i assign to chat screen class.

1

1 Answers

0
votes

You need to learn:

  1. XMPPMessageArchiving is an example module bundled with XMPPFramework which store all incoming and outgoing messages to CoreData-backed database.
  2. NSFetchedResultsController - standard Apple-provided class for feeding data to UITableView or UICollectionView, which you can use as core of "chat screen" class and it will automatically track new messages and update UI.

Sample iPhoneXMPP project have an example of "roster screen" which is backed by UITableView and NSFRC, the only two differences of "chat screen" are:

  1. You need to fetch XMPPMessageArchiving_Message_CoreDataObject entities
  2. You need to specify NSPredicate to fetch only messages related to single "chat", something like bareJid == %@ and pass buddy's JID to this predicate