XMPP finishing notes: Sending and displaying chat messages

Source: Internet
Author: User

The transmission of any information needs to focus on two parts, the issue of Information , and the display of information in the interface

Past Period Review:

XMPP Grooming Notes: Quick configuration of the environment (with installation package) http://www.cnblogs.com/dsxniubility/p/4304570.html

XMPP Grooming Notes: Xmppframework framework Import and introduction http://www.cnblogs.com/dsxniubility/p/4307057.html

XMPP Grooming Notes: User network connection and friend management http://www.cnblogs.com/dsxniubility/p/4307066.html

One thing to note:

Chat Interface Input box is not TextField but TextView, because TextView can control the multi-line information between the up and down scrolling editing, if the following chat box with the ordinary TextField you can imagine what will happen.

Use the simulator program and chat with the ducklings as follows:

If you are not in the Dong Platinum Blog Park See this article please click to view the original

One. Sending of informationFirst, you have a result scheduler

This is the necessary action to fetch data from Sqllite through CoreData. All code is written in this lazy load

-(Nsfetchedresultscontroller *) Fetchedresultscontroller {//recommended notation, reduce nested hierarchy if (_fetchedresultscontroller! = nil) {    return _fetchedresultscontroller; }//First determine which entity is required nsfetchrequest *request = [Nsfetchrequest fetchrequestwithentityname:@ "Xmppmessagearchiving_mes        Sage_coredataobject "];    Sort Nssortdescriptor *sort = [Nssortdescriptor sortdescriptorwithkey:@ "timestamp" ascending:yes];        Request.sortdescriptors = @[sort];        Every chat interface, only concerned about the message of the chat object request.predicate = [Nspredicate predicatewithformat:@ "barejidstr =%@", Self.chatJID.bare]; Get context Nsmanagedobjectcontext from the attributes in the tool class you wrote *ctx = [Sxxmpptools Sharedxmpptools].xmppmessagearchivingcoredatastora        Ge.mainthreadmanagedobjectcontext; Instantiation, the inside to fill in the above various parameters _fetchedresultscontroller = [[Nsfetchedresultscontroller alloc] Initwithfetchrequest:request    Managedobjectcontext:ctx Sectionnamekeypath:nil Cachename:nil];        _fetchedresultscontroller.delegate = self; Return _fetChedresultscontroller;} 

After writing the results of the scheduler to remember in the Viewdidload page first load add a sentence, or do not work

Querying data    [Self.fetchedresultscontroller performfetch:null];

send out a message

Because TextView does not have a TextField-like Shouldreturn method to send messages directly

So can only TextView " proxy method of text change method " a little processing to achieve the purpose of text delivery

#pragma mark-******************** TextView Proxy Method-(BOOL) TextView: (Uitextview *) TextView Shouldchangetextinrange: ( Nsrange) Range Replacementtext: (NSString *) text{    //Determine if the hit is not the ENTER key.    if ([text isequaltostring:@ "\ n"]) {                //Custom message sending method, the incoming string is emitted directly.        [self sendMessage:textView.text];                Self.textView.text = nil;                return NO;    }    return YES;}

If you press ENTER when entering the character that triggers the Proxy method is "\ n" will call the SendMessage method that you wrote, passing in a string to send directly.

This method internally addbody the incoming text and then sends it out with the XMPP stream of its own handwritten singleton class.

#pragma mark-******************** Send Message method/** send message */-(void) SendMessage: (NSString *) message{    xmppmessage *msg = [XMP PMessage messagewithtype:@ "chat" To:self.chatJID];        [MSG addbody:message];        [[Sxxmpptools Sharedxmpptools].xmppstream sendelement:msg];}

Two. The display of information in TableView

is in the TableView data source method, first remove the current information from the database, and then determine whether to send or receive. Take a different reusable identifier, and then assign a value

    Remove the current line message    xmppmessagearchiving_message_coredataobject *message = [Self.fetchedresultscontroller objectatindexpath:indexpath];//to determine whether to send a message or receive a message    nsstring *id = ([message.outgoing intvalue] = = 1)? @ "Sendcell": @ "Rec Ivecell ";    Sxchatcell *cell = [TableView dequeuereusablecellwithidentifier:id];    Cell.messageLabel.text = Message.body;

The class names that SX opened are my own custom classes, which are usually written in the usual notation.

In the above mentioned tool class Sxxmpptools This module needs to use the properties and methods have

/** XMPP Stream */@property (nonatomic,strong,readonly) xmppstream * xmppstream;/** Message archive */@property (nonatomic, Strong, READONLY) xmppmessagearchiving *xmppmessagearchiving;/** Message Archive storage */@property (nonatomic, Strong, ReadOnly) Xmppmessagearchivingcoredatastorage *xmppmessagearchivingcoredatastorage;+ (instancetype) sharedXMPPTools;

Here's one thing to note,

Because by default, when you enter a friend's chat page, you and all your friends ' chats will be displayed. Because there are all the same data table, so we need to do a layer of filtering, that is lazy loading in this line of code

Every chat interface, only concerned about the message of the chat object    request.predicate = [Nspredicate predicatewithformat:@ "barejidstr =%@", Self.chatJID.bare] ;

If you are not in the Dong Platinum Blog Park See this article please click to view the original

is organizing the audio file and picture file sending method, interested can pay attention to me.

XMPP finishing notes: Sending and displaying chat messages

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.