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 archive storage model @property (Nonatomic,strong) Xmppmessagearchivingcoredatastorage * Messagestore;
4. In the controller created above storyboard, add the following code
Appdelegate * Delgate=[uiapplication sharedapplication].delegate; Initialize Avatar Xmppjid * 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 Picture model NSData * 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: Uikeyboardwillshownotification Object:nil]; [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (hidekey:) Name:uikeyboardwillhidenoTification Object:nil]; Initialize data storage NSString *user= [[nsuserdefaults standarduserdefaults] objectforkey:@ "UserName"]; Initialization Request Nsfetchrequest * Request=[[nsfetchrequest alloc] initwithentityname:@ "Xmppmessagearchiving_message_ Coredataobject "]; Request.predicate=[nspredicate predicatewithformat:@ "barejidstr=%@ and streambarejidstr=%@", Self.fromJid,user]; Define Sort Nssortdescriptor * des=[nssortdescriptor sortdescriptorwithkey:@ "timestamp" ascending:yes]; [Request Setsortdescriptors:@[des]]; Get context Nsmanagedobjectcontext *context=[delgate.messagestore Mainthreadmanagedobjectcontext]; Initialize result memory Fetch=[[nsfetchedresultscontroller alloc] Initwithfetchrequest:request managedobjectcontext:context Sectionnamekeypath:nil Cachename:nil]; Set proxy fetch.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 Avatar cell.image.image=self.meimage; Cell.selectionstyle=uitableviewcellselectionstylenone; [Cell SetText:obj.body WithFlag:obj.isOutgoing]; return cell;}
6. Increase Keyboard control
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (showKey:) Name: Uikeyboardwillshownotification Object:nil]; [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (hidekey:) Name: Uikeyboardwillhidenotification object: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 take out text appdelegate * delgate=[uiapplication sharedapplication].delegate; Xmppjid * Jid=[xmppjid JidWithString:self.fromJid]; Initialize the body of the message xmppmessage * message=[xmppmessage messagewithtype:@ "chat" To:jid]; [Message AddBody:self.ketf.text]; Send Message [Delgate.stream sendelement:message]; Empty the message Self.ketf.text=nil; return YES;}
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://blog.csdn.net/jerehedu/
This article is the copyright of Yantai Jerry Education Technology Co., Ltd. and CSDN Common, welcome reprint, but without the author's consent must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
XMPP accepts sending messages