Storing messages using XMPPFramework for ios? Storing messages using XMPPFramework for ios? ios ios

Storing messages using XMPPFramework for ios?

I know this is an old thread but as I am currently working with XMPP on iOS I must say that there is a built in support for archiving messages in XMPP.

I downloaded the XMPP framework for iOS and in it there is folder marked XEP-0136. Import the folders in XCode and activate client side archiving by using the following lines of code in the class you instantiate XMPP client:

xmppMessageArchivingStorage = [XMPPMessageArchivingCoreDataStorage sharedInstance];xmppMessageArchivingModule = [[XMPPMessageArchiving alloc] initWithMessageArchivingStorage:xmppMessageArchivingStorage];

the following one line of code saves you from sending archive specific stanzas to the xmpp serverwhich will most probably respond with service-not-implemented

[xmppMessageArchivingModule setClientSideMessageArchivingOnly:YES];[xmppMessageArchivingModule activate:xmppStream];[xmppMessageArchivingModule  addDelegate:self delegateQueue:dispatch_get_main_queue()];

And you are set. From that moment on, messages (outgoing and incoming) will be stored in a table created by the framework.

If you need more info please comment and i will get back to you.


I did like this :

-(void)testMessageArchiving{            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];            [self print:[[NSMutableArray alloc]initWithArray:messages]];}-(void)print:(NSMutableArray*)messages{         @autoreleasepool {            for (XMPPMessageArchiving_Message_CoreDataObject *message in messages) {                NSLog(@"messageStr param is %@",message.messageStr);                NSXMLElement *element = [[NSXMLElement alloc] initWithXMLString:message.messageStr error:nil];                NSLog(@"to param is %@",[element attributeStringValueForName:@"to"]);                NSLog(@"NSCore object id param is %@",message.objectID);                NSLog(@"bareJid param is %@",message.bareJid);                NSLog(@"bareJidStr param is %@",message.bareJidStr);                NSLog(@"body param is %@",message.body);                NSLog(@"timestamp param is %@",message.timestamp);                NSLog(@"outgoing param is %d",[message.outgoing intValue]);            }        }}

Hope it helps :)

The responses that indicate XMPP Framework doesn't save the history are incorrect.

To integrate results in a table view use:

XMPPMessageArchivingCoreDataStorage *storage = [XMPPMessageArchivingCoreDataStorage sharedInstance];NSManagedObjectContext *moc = [storage mainThreadManagedObjectContext];NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"XMPPMessageArchiving_Contact_CoreDataObject"                                                     inManagedObjectContext:moc];NSFetchRequest *request = [[NSFetchRequest alloc]init];[request setEntity:entityDescription];_contactsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:moc sectionNameKeyPath:nil cacheName:@"MessagesContactListCache"];NSError *error;BOOL rval = [_contactsController performFetch:&error];

an example to get archived messages in Swift 4

declares and initializes the variables XMPPMessageArchivingCoreDataStorage where I initialize the XMPPStream

    var xmppMessageStorage: XMPPMessageArchivingCoreDataStorage?    var xmppMessageArchiving: XMPPMessageArchiving?    xmppMessageStorage = XMPPMessageArchivingCoreDataStorage.sharedInstance()        xmppMessageArchiving = XMPPMessageArchiving(messageArchivingStorage: xmppMessageStorage)        xmppMessageArchiving?.clientSideMessageArchivingOnly = true        xmppMessageArchiving?.activate(stream)        xmppMessageArchiving?.addDelegate(self, delegateQueue: DispatchQueue.main)

doing this, whenever a message arrives, this will cause it to be archived without needing to do anything else.

then, to retrieve the archived message

func RecibedMessageArchiving(idFriend: String) {        let JabberIDFriend = idFriend   //id friend chat, example        let moc = xmppMessageStorage?.mainThreadManagedObjectContext        let entityDescription = NSEntityDescription.entity(forEntityName: "XMPPMessageArchiving_Message_CoreDataObject", in: moc!)        let request = NSFetchRequest<NSFetchRequestResult>()        let predicateFormat = "bareJidStr like %@ "        let predicate = NSPredicate(format: predicateFormat, JabberIDFriend)        request.predicate = predicate        request.entity = entityDescription        //jabberID id del usuario, cliente        var jabberIDCliente = ""        if let jabberj = globalChat.value(forKey: "jabberID"){            jabberIDCliente = jabberj as! String        }        do {            let results = try moc?.fetch(request)            for message: XMPPMessageArchiving_Message_CoreDataObject? in results as? [XMPPMessageArchiving_Message_CoreDataObject?] ?? [] {                var element: DDXMLElement!                do {                    element = try DDXMLElement(xmlString: (message as AnyObject).messageStr)                } catch _ {                    element = nil                }                let body: String                let sender: String                let date: NSDate                let isIncomings: Bool                if message?.body != nil {                    body = (message?.body)!                } else {                    body = ""                }                if element.attributeStringValue(forName: "to") == JabberIDFriend {                    sender = jabberIDCliente                    isIncomings = false                } else {                    sender = ""                    isIncomings = true                }                    var m: [AnyHashable : Any] = [:]                    m["msg"] = message?.body                    print("body", message?.body)                    print("test", element.attributeStringValue(forName: "to"))                    print("test2", element.attributeStringValue(forName: "body"))            }        } catch _ {            //catch fetch error here        }    }