Xmpp sort notes: Send and display chat information, xmpp sort notes

Source: Internet
Author: User

Xmpp sort notes: Send and display chat information, xmpp sort notes

The sending of any information requires attention to two parts: information issuance and information display on the interface.

Previous review:

Xmpp Organization notes: Quick environment configuration (with installation package) http://www.cnblogs.com/dsxniubility/p/4304570.html

Xmpp Organization notes: import and introduction to xmppFramework http://www.cnblogs.com/dsxniubility/p/4307057.html

Xmpp Organization notes: user network connection and friend management http://www.cnblogs.com/dsxniubility/p/4307066.html

Note that:

The text box in the chat interface is not textField but textView, because textView can control the up and down scrolling edit between multiple rows of information, if the following chat box uses a common textField, you can imagine what will happen.

Use a simulator program to chat with duck as follows:

If you did not see this article in Dong Boran's blog Park, click to view the original article.

 

I. Before sending information, you must have a result scheduler.

This is a necessary operation to retrieve data from SQLlite through coredata. Write all the code in this lazy load.

-(NSFetchedResultsController *) fetchedResultsController {// recommended syntax to reduce the nested hierarchy if (_ fetchedResultsController! = Nil) {return _ fetchedResultsController;} // determine the entity to use NSFetchRequest * request = [NSFetchRequest fetchRequestWithEntityName: @ "XMPPMessageArchiving_Message_CoreDataObject"]; // sort NSSortDescriptor * sort = [NSSortDescriptor sortDescriptorWithKey: @ "timestamp" ascending: YES]; request. sortDescriptors = @ [sort]; // each chat interface only cares about the message request of the chat object. predicate = [NSPredicate predicateWithFormat: @ "bareJidStr = % @", self. chatJID. bare]; // obtain the context NSManagedObjectContext * ctx = [SXXMPPTools sharedXMPPTools] From the attributes in the tool class you write. xmppMessageArchivingCoreDataStorage. parameters; // instantiation, which must be filled with the above parameters _ fetchedResultsController = [[delealloc] initWithFetchRequest: request managedObjectContext: ctx sectionNameKeyPath: nil cacheName: nil]; _ fetchedResultsController. delegate = self; return _ fetchedResultsController ;}

 

After writing the result scheduler, remember to add a sentence to the first load on the viewdidload page; otherwise, do not work.

// Query data [self. fetchedResultsController into mfetch: NULL];

 

Send a message

Because there is no shouldReturn method similar to textField in textView to directly send information

Therefore, you can only process textView's proxy method text change method slightly to achieve text sending.

# Pragma mark-********************* textView proxy method-(BOOL) textView :( UITextView *) textView shouldChangeTextInRange :( nsange) range replacementText :( NSString *) text {// determines whether the press is a carriage return key. If ([text isEqualToString: @ "\ n"]) {// custom message sending method, the input string is sent directly. [Self sendMessage: textView. text]; self. textView. text = nil; return NO;} return YES ;}

If you press the Enter key during input, the "\ n" character that triggers the proxy method is passed in. Then, the sendMessage method you write is called and a string is passed in to send it directly.

 

In this method, the incoming text is first addBody and then sent to the xmpp stream of the handwritten Singleton class.

# Pragma mark-********************* message sending method/** message sending */-(void) sendMessage :( NSString *) message {XMPPMessage * msg = [XMPPMessage messageWithType: @ "chat" to: self. chatJID]; [msg addBody: message]; [[SXXMPPTools sharedXMPPTools]. xmppStream sendElement: msg];}

 

II. Display Information in tableView

In the tableView data source method, the current information is first retrieved from the database, and then the result is returned or received. Get different reusable identifiers and assign values

// Retrieve the message XMPPMessageArchiving_Message_CoreDataObject * message = [self. fetchedResultsController objectAtIndexPath: indexPath]; // determines whether the message is sent or received. NSString * ID = ([message. outgoing intValue] = 1 )? @ "SendCell": @ "ReciveCell"; SXChatCell * cell = [tableview dequeueReusableCellWithIdentifier: ID]; cell. messageLabel. text = message. body;

Specifically, the class names opened by sx are all self-defined classes, which are all common writing methods.

 

The attributes and methods required for this module in the tool class SXXmppTools mentioned above are as follows:

/** Xmpp stream */@ property (nonatomic, strong, readonly) XMPPStream * xmppStream;/** message archiving */@ property (nonatomic, strong, readonly) XMPPMessageArchiving * handle; /** archive message storage */@ property (nonatomic, strong, readonly) XMPPMessageArchivingCoreDataStorage * xmppMessageArchivingCoreDataStorage; + (instancetype) sharedXMPPTools;

 

Note that,

By default, when you enter the chat page of a friend, the chat records of you and all friends are displayed. Because they all exist in the same data table, a layer of filtering is required, that is, this line of code in the lazy loading.

// For each chat interface, only the request. predicate = [NSPredicate predicateWithFormat: @ "bareJidStr = % @", self. chatJID. bare] of the chat object is concerned.

If you did not see this article in Dong Boran's blog Park, click to view the original article.

I am sorting out how to send audio and image files. If you are interested, please pay attention to me.

Related Article

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.