Message:
Then write
. H
1 /*! 2 * @Author Dylan. 3 * 4 * callback Block 5 */ 6 typedef void(^sendSuccess)(); 7 typedef void(^sendFailure)(id); 8 9 /*!10 * sendMessageBy model11 */12 - (void)sendMessage: (ADMessageModel *)message13 sendSuccess: (sendSuccess)success14 sendFailure: (sendFailure)failure;15 16 /*!17 * @Author Dylan.18 *19 * unRead Msg20 */21 @property (nonatomic, strong) NSMutableDictionary * unReadMsg;22 23 /*!24 * @Author Dylan.25 *26 * new Msg27 */28 @property (nonatomic, copy) void (^newMessage) (id);29 30 31 @end
. M
1 # pragma mark-initdata 2-(void) initdata {3 // data persistence 4 self. unreadmsg = [invalid dictionary]; 5} 6 7 # pragma mark Methods 8-(void) sendmessage: (admessagemodel *) Message 9 sendsuccess: (sendsuccess) success10 sendfailure: (sendfailure) failure {11 12 // set callback block13 self. success = success; 14 self. failure = failure; 15 16 nsxmlelement * Body = [nsxmlelement elementwithname: @ "Body"]; 17 [Body setstringvalue: Message. body]; 18 19 // generate XML message Document 20 nsxmlelement * MEs = [nsxmlelement elementwithname: @ "message"]; 21 // Message Type 22 [mes addattributewithname: @ "type" stringvalue: @ "chat"]; 23 // to whom 24 [mes addattributewithname: @ "to" stringvalue: Message. to]; 25 // who sent 26 [mes addattributewithname: @ "from" stringvalue: Message. from]; 27 // combination 28 [mes addchild: body]; 29 // send message 30 [self xmppstream] Sen Delement: MES]; 31} 32 33 # pragma mark-delegetemethods34-(void) xmppstream :( xmppstream *) sender didreceivemessage :( xmppmessage *) message {35 36 nsstring * Body = [[Message elementforname: @ "body"] stringvalue]; 37 nsstring * From = [[Message attributeforname: @ "from"] stringvalue]; 38 39 if (body! = Nil) {40 41 nsmutabledictionary * msgdict = [nsmutabledictionary dictionary]; 42 admessagemodel * model = [[admessagemodel alloc] init]; 43 model. body = body; 44 model. from = from; 45 [msgdict setvalue: Model forkey: [adcurrenttime getcurrenttime]; 46 47 If ([from isequaltostring: [[nsuserdefaults standarduserdefaults] stringforkey: current_chat]) {48 49 self. newmessage (msgdict); 50} else {51 // not current chat52 if ([_ unreadmsg. allkeys containsobject: From]) {53 [_ unreadmsg [from] addobject: Model]; 54} else {55 [_ unreadmsg setvalue: [nsmutablearray arraywithobject: msgdict] forkey: From]; 56} 57} 58 59} 60} 61 62 @ end
XMPP logon to register a friend group _ practical tutorial 2