1
votes

We are using Amazon Ubuntu Server, Apache and PHP Installed Openfire of version 4.0.1 and the app configurations are XCode 7.2 by using Swift and library we are using is XMPP Framework of version 3.6.6. We can able do single text chat, but cannot store chat history - How can we maintain chat history and retrieve that.

2

2 Answers

4
votes
  • Create a XCMODEL schema file of core data in your project
  • Add your entities in that model. That can be in your case

    • MESSAGE entity
    • CONTACT entity
    • etc (whatever you want to save )
    • For each entity in your DB model, you can now add "Attributes" in your Entity (say MESSAGE entity). Attributes can be created time, message text etc.
    • Make a NSObject extended class which have properties same as your attributes
    .h file
        @property (nonatomic, retain) NSNumber * createdAt;         
    
    .m file
        @dynamic createdAt;
    

Now you can use : Core data operation methods like saving NS managed object etc to save or other operation on DB.

For Example : to save a contact in your DB with full name attribute.

DBHandler * dbHandler = [DBHandler sharedInstance];

DB_CONTACT* existingContact = [self getContactByKey:@"userId" value:[userContact userId]];
if (existingContact) {
    return false;
}  

BOOL result = NO;

DB_CONTACT * contact = [NSEntityDescription insertNewObjectForEntityForName:@"DB_CONTACT"
inManagedObjectContext:dbHandler.managedObjectContext];

contact.fullName = userContact.fullName;
NSError *error = nil;

result = [dbHandler.managedObjectContext save:&error];

if (!result) {
    NSLog(@"DB ERROR :%@",error);
}

return result;

Method responsible to save in DB:

[NSEntityDescription insertNewObjectForEntityForName:@"DB_CONTACT"
inManagedObjectContext:dbHandler.managedObjectContext];

Hope this gives you an idea. For more details check https://github.com/AppLozic/Applozic-iOS-SDK out as sample app for messaging.

0
votes

If you want to maintain chat local history, you can use coredata to save all incoming and send messages on the right order. To restore the chat history, use parameters like XMPPUserName (from and to) from your coredata. To see the history on a openfire server, you can: Install the Monitoring Server plugin Go to the tab “Archiving” Select their settings and enable "Archive one-to-one chats”