XMPP accepts sending messages

Source: Internet
Author: User

At this stage of communication services, all kinds of standards have, so there will be unable to achieve interconnection, and XMPP (extensible Message and Presence Protocol) protocol, the realization of the entire timely Communication Service protocol interoperability. With this agreement, the use of any one organization or individual, even if the communication services, can be accessible to other users of the timely communication services to communicate. Google Talk, for example, launched in 2005, is an XMPP protocol-based instant messaging software. Let's talk about how to simply use XMPP to receive and send messages

1, in the XMPPFramework.h will need to use the header file open

2. Create a display message page in storyboard

3. Define the query result memory and initialize it.
// Message archive @property (nonatomic,strong) xmppmessagearchiving * Messagearch; // message archiving Storage model @property (nonatomic,strong) xmppmessagearchivingcoredatastorage * messagestore;
4. In the controller created above storyboard, add the following code
Appdelegate * Delgate=[uiapplication Sharedapplication].Delegate; //Initialize AvatarXmppjid * fromjid=[Xmppjid JidWithString:self.fromJid]; NSData* Fromdata=[Delgate.vcardavatarmodule Photodataforjid:fromjid]; Self.fromimage=[[UIImage alloc] initwithdata:fromdata]; NSString* Username= [[nsuserdefaults standarduserdefaults] Objectforkey:@"UserName"]; Xmppjid* tojid=[Xmppjid Jidwithstring:username]; //set up a picture modelNSData * Todata=[Delgate.vcardavatarmodule Photodataforjid:tojid]; Self.meimage=[[UIImage alloc] initwithdata:todata]; if(self.fromimage==Nil) {Self.fromimage=[uiimage imagenamed:@"Defalut"]; }        if(self.meimage==Nil) {Self.meimage=[uiimage imagenamed:@"Defalut"]; } [[Nsnotificationcenter defaultcenter] addobserver:self selector: @selector (showKey:) Name: UikeyboardwillshownotificationObject: nil]; [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (hidekey:) Name: UikeyboardwillhidenotificationObject: nil]; //initializing the data storeNSString *user= [[Nsuserdefaults standarduserdefaults] Objectforkey:@"UserName"]; //Initializing requestsNsfetchrequest * Request=[[nsfetchrequest alloc] Initwithentityname:@"Xmppmessagearchiving_message_coredataobject"]; Request.predicate=[nspredicate Predicatewithformat:@"barejidstr=%@ and streambarejidstr=%@", Self.fromjid,user]; //Define SortNssortdescriptor * Des=[nssortdescriptor Sortdescriptorwithkey:@"timestamp"Ascending:yes];        [Request Setsortdescriptors:@[des]]; //Get ContextNsmanagedobjectcontext *context=[Delgate.messagestore Mainthreadmanagedobjectcontext]; //Initialize result memoryFetch=[[Nsfetchedresultscontroller alloc] Initwithfetchrequest:request managedobjectcontext:context        Sectionnamekeypath:nil Cachename:nil]; //Set up proxyFetch.Delegate=Self ; //Start Query[Fetch Performfetch:nil]; 
5, create the agent method to realize TableView
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{ID<NSFetchedResultsSectionInfo> info=Fetch.sections[section]; NSLog (@"===%ld", info.numberofobjects); return[Info numberofobjects]; }-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{Xmppmessagearchiving_message_coredataobject* obj=[Fetch Objectatindexpath:indexpath]; Jrchattableviewcell* cell=Nil; if(obj.isoutgoing) {cell=[tableview Dequeuereusablecellwithidentifier:@"Cellto"]; }Else{cell=[tableview Dequeuereusablecellwithidentifier:@"Cellfrom"]; }            //Set AvatarCell.image.image=Self.meimage; Cell.selectionstyle=Uitableviewcellselectionstylenone;    [Cell SetText:obj.body WithFlag:obj.isOutgoing]; returncell;}
6. Increase Keyboard control
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (showKey:) Name: UikeyboardwillshownotificationObject: nil]; [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (hidekey:) Name: UikeyboardwillhidenotificationObject: nil];- (void) ShowKey: (Nsnotification *) notify{cgfloat Time=[Notify.userinfo[uikeyboardanimationdurationuserinfokey] doublevalue]; CGRect Frame=[Notify.userinfo[uikeyboardframebeginuserinfokey] cgrectvalue]; [UIView animatewithduration:time animations:^{self.keyView.transform=cgaffinetransformmaketranslation (0, frame.size.height*-1); }];}- (void) Hidekey: (Nsnotification *) notify{cgfloat Time=[Notify.userinfo[uikeyboardanimationdurationuserinfokey] doublevalue]; [UIView animatewithduration:time animations:^{self.keyView.transform=cgaffinetransformidentity; }];}
7. Send a message
-(BOOL) Textfieldshouldreturn: (Uitextfield *) textfield{[TextField Resignfirstresponder]; //Send Message//1 Remove TextAppdelegate * Delgate=[uiapplication Sharedapplication].Delegate; Xmppjid* jid=[Xmppjid JidWithString:self.fromJid]; //Initialize the body of the messageXmppmessage * Message=[xmppmessage Messagewithtype:@"Chat"To:jid];        [Message AddBody:self.ketf.text]; //Send Message[Delgate.stream Sendelement:message]; //Empty the messageself.ketf.text=Nil; returnYES;}

To learn more about the small partners, you can click to view the source code , run the test yourself.

Inquiries or technical exchanges, please join the official QQ Group: (452379712)

Jerry Education
Source:http://www.cnblogs.com/jerehedu/
This article is the copyright of Yantai Jerry Education Technology Co., Ltd. and the blog Park is shared, welcome reprint, but without the consent of the author must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

XMPP accepts sending 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.