Original address: xmppframework iOS development (4)
Message
// Receive the message-(void) xmppstream :( xmppstream *) sender didreceivemessage :( xmppmessage *) Message {// nslog (@ "message = % @", message ); // message content nsstring * MSG = [[Message elementforname: @ "body"] stringvalue]; // message sender nsstring * From = [[Message attributeforname: @ "from"] stringvalue];/***** here ***** // The Notification chat page contains new messages, which need to be processed}
Send message
// XML format for sending a message <message from = 'sender account' to = 'receiver account' type = 'chat'> <body> Hello World </body> </message>
//CodeAssembly
Nsstring * message = @ "Hello World"; nsxmlelement * Body = [nsxmlelement elementwithname: @ "body"]; [Body setstringvalue: Message]; // generate the XML message document nsxmlelement * MEs = [nsxmlelement elementwithname: @ "message"]; // Message Type [mes addattributewithname: @ "type" stringvalue: @ "chat"]; // who will send the message to [mes addattributewithname: @ "to" stringvalue: @ "recipient account"]; // who will send the message [mes addattributewithname: @ "from" stringvalue: @ "sender account"]; // combine [mes addchild: body]; // send a message [[self xmppstream] sendelement: MES];
Online/offline notifications of friends
-(Void) xmppstream :( xmppstream *) sender didreceivepresence :( xmpppresence *) Presence {// obtain the friend status nsstring * presencetype = [presence type]; // online/offline // The current user nsstring * userid = [[Sender myjid] user]; // The online user nsstring * presencefromuser = [[presence from] user]; /* // if you are not yourself, you 'd better add else here if you are not yourself. If you are offline, call the online protocol xmpppresence * presence = [xmpppresence presence]; [[self xmppstream] sendelement: Presence]; */If (! [Presencefromuser isinclutostring: userid]) {// user online if ([presencetype isinclutostring: @ "available"]) {// The list and database must be changed accordingly} else if ([presencetype isequaltostring: @ "unavailable"]) // The user is offline {// The list and database must be changed accordingly }}}