Advanced iOS Learning-Instant Messaging (2)

Source: Internet
Author: User
Tags file url

This is a continuation of instant messaging (1).

  First, friends List

  The Manage Friends list is through the Roster (Xmpproster) in XMPP. code example:

  #pragma  Mark-Manage Friends //  Get a singleton object that manages friends  xmpprostercoredatastorage *rosterstorage = [Xmpprostercoredatast        Orage Sharedinstance];         //  Initialize the Roster property  Self.xmpproster = [[Xmpproster alloc] Initwithrosterstorage:rosterstorage dispatchqueue:dispatch_get_global_queue (        Dispatch_queue_priority_background, 0   //  Activate the buddy list in the channel   [Self.xmpproster Activate:self.xmppStream];  //  set roster agent  [Self.xmpproster Adddelegate:self Delegatequeue:dispatch_get_main_queue ()]; 

Xmpprosterdelegate Proxy Method:

  

  Add Friends:

  

To delete a friend:

Some code examples:

#pragmaMark-xmpprosterdelegate Proxy method//start getting friends- (void) Xmpprosterdidbeginpopulating: (Xmpproster *) sender{NSLog (@"start getting friends");}//End Get Friends- (void) Xmpprosterdidendpopulating: (Xmpproster *) sender{NSLog (@"End Get Friends");}//receive information about your friends- (void) Xmpproster: (Xmpproster *) sender Didreceiverosteritem: (Ddxmlelement *) item{//display information for friend statusNSLog (@"Friend information = =%@", item);}//Monitoring Methods- (void) Xmpproster: (Xmpproster *) sender Didreceivepresencesubscriptionrequest: (Xmpppresence *) presence{NSLog (@"get a Friend request");}

Second, get chat information

1, the chat rules:

    • Gets the chat record from the server and determines the message type based on the data attributes.
    • Send a message.
    • Receives the message.
    • code example:
// initializing a message archive object Xmppmessagearchivingcoredatastorage*messagestorage == [[Xmppmessagearchiving alloc] Initwithmessagearchivingstorage:messagestorage Dispatchqueue:dispatch_get_main_queue ()]; // Activate [Self.messagearchiving Activate:self.xmppStream];

2. Get Chat History:

    • get a chat record using CoreData way.
    • Create request.
    • Create entity description, Entity name: Xmppmessagearchiving_ Message_coredataobject.
    • create predicate query condition: streambarejidstr = = I jid and barejidstr = = Friend Jid.
    • Create sort objects, sort criteria: timestamp.
    • Execute request.
    • code example:
    Xmppmessage *message = [xmppmessage messagewithtype:@ "chat"  To:[xmppjid jidwithstring : SELF.CHATTOSB Resource:kresource]];    [Message addbody:@ "hello!!! "];    // using sendelement: Sending data to the server    [[Xmppmanager Defaultmanager].xmppstream sendelement:message];

Iii. Sending and receiving messages

Callback to receive/Send message:

// Send Message succeeded -(void) Xmppstream: (Xmppstream *) sender Didsendmessage: (xmppmessage *) message{   }  //  Receive Message Success -(void) Xmppstream: (Xmppstream *) sender Didreceivemessage: (Xmppmessage * ) message{    }//  Send message failed -(void) Xmppstream: (Xmppstream *) sender Didfailtosendmessage: (xmppmessage *) message Error: (Nserror *) error{    }

Four, session bubble implementation

  

Five, the principle of voice pictures

Transmission principle:

1. Upload the picture/voice to the server.

2, according to the contract with the server, to spell the file in the server address (that is, the image or voice URL).

3, call XMPP send information method, send the address out.

4, received at the receiving end of a text message, which is just a pointer to the resource file URL address.

5, after getting the URL to do the necessary action (that is, request a picture or voice display on the page).

  Vi. examples

  According to the two days of learning to improve the code of yesterday to achieve a simple instant messaging system. I uploaded the code to GitHub. Here for everyone to see the completion of the effect (because it is simple, so just realize the function, the interface is very low~~~ you can on the basis of their preferences to beautify ^_^):

Advanced iOS Learning-Instant Messaging (2)

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.