Start today to do the most important functions to send and receive messages, to get local historical data.
First up to date:
The first is to introduce a data storage module into the XMPPFramework.h:
Import of the chat log module #import "XMPPMessageArchiving.h" #import "XMPPMessageArchivingCoreDataStorage.h" #import " Xmppmessagearchiving_contact_coredataobject.h "//Closest contact #import" Xmppmessagearchiving_message_coredataobject.h "
The data storage module is then added to the Xmppstream because the XMPP framework defaults to storing the data with CoreData, and the data is stored, so we just need to send the notification processing information in the right place.
4. Message module, here with a single case, can not switch account login, otherwise there will be data problems. _xmppmessagearchivingcoredatastorage = [Xmppmessagearchivingcoredatastorage sharedinstance]; _xmppmessagearchiving = [[Xmppmessagearchiving alloc] Initwithmessagearchivingstorage:_ Xmppmessagearchivingcoredatastorage Dispatchqueue:dispatch_get_global_queue (Dispatch_queue_priority_background, 9)]; [_xmppmessagearchiving Activate:self.xmppStream];
After the data store is complete, send a notification:
in the Xmppmessagearchivingcoredatastorage's
-(void) archivemessage: ( Xmppmessage *) Message outgoing: (BOOL) isoutgoing xmppstream: (xmppstream *) Add the following notification at the end of the Xmppstream:
Dispatch_async (Dispatch_get_main_queue (), ^{ [[Nsnotificationcenter Defaultcenter] Postnotificationname: Kxmpp_message_change Object:nil]; });
Now go to the chat interface to get historical news:
/** Query chat record */-(void) getchathistory{xmppmessagearchivingcoredatastorage *storage = [Jkxmpptool SHAREDINSTANCE].XMPPME Ssagearchivingcoredatastorage; Query the time to give context nsfetchrequest *fetchrequest = [[Nsfetchrequest alloc] init]; Nsentitydescription *entity = [nsentitydescription entityForName:storage.messageEntityName inmanagedobjectcontext: Storage.mainthreadmanagedobjectcontext]; [Fetchrequest setentity:entity]; Specify criteria for filtering which objects to fetch nspredicate *predicate = [nspredicate predicatewithformat:@ "ba Rejidstr =%@ ", Self.chatJID.bare]; [Fetchrequest Setpredicate:predicate]; Specify how the fetched objects should be sorted nssortdescriptor *sortdescriptor = [[Nssortdescriptor alloc] Initwi thkey:@ "timestamp" ascending:yes]; [Fetchrequest Setsortdescriptors:[nsarray Arraywithobjects:sortdescriptor, Nil]]; Nserror *error = nil; Nsarray *fetchedobjects = [Storage.mainthreadmanagedobjectcontext executefetchrequest:fetchrequest error:&error]; if (fetchedobjects! = nil) {self.chathistory = [[Nsmutablearray alloc] initwitharray:fetchedobjects]; [Nsmutablearray arraywitharray:fetchedobjects]; } [Self.messagetableview Reloaddata]; [Self tableviewscrolltobottom];}
message ingestion is also called Getchathistory method can be
And the message is sent:
/** sent event */-(void) sendmessage{ if (_chattextfield.text.length < 1) { return; } Xmppmessage *message = [xmppmessage messagewithtype:@ "chat" To:self.chatJID]; [Message Addbody:_chattextfield.text]; [[Jkxmpptool Sharedinstance].xmppstream sendelement:message]; _chattextfield.text = @ ""; [Self tableviewscrolltobottom];}
Demo Address: Https://github.com/Joker-King/ChatDemo
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
XMPP series (iv)---Send and receive text messages for historical message functionality