This mainly refers to the user's status update, receiving a message, and sending a message.
First, provide the core code
User online/offline notifications
1 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 2 #pragma mark ReceivePresence 3 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 4 5 - (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence { 6 NSString *presenceType = [presence type]; 7 NSString *presenceFromUser = [[presence from] user]; 8 NSLog(@"presenceFromUser:%@",presenceFromUser); 9 NSLog(@"presenceType:%@",presenceType);10 if (![presenceFromUser isEqualToString:[[sender myJID] user]]) {11 if ([presenceType isEqualToString:@"available"]) {12 //13 } else if ([presenceType isEqualToString:@"unavailable"]) {14 //15 }16 }17 }
Receive messages
1 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 2 #pragma mark ReceiveMessage 3 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 4 5 - (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message { 6 NSString *messageBody = [[message elementForName:@"body"] stringValue]; 7 NSLog(@"body:%@",messageBody); 8 NSString *replyMessage=[NSString stringWithFormat:@"replay%@",messageBody]; 9 if(![messageBody isEqual:NULL])10 [self sendMessage:replyMessage toUser:@"fengyun"];11 }
Send message
1 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 2 #pragma mark sendMessage 3 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 4 5 - (void)sendMessage:(NSString *) message toUser:(NSString *) user { 6 NSXMLElement *body = [NSXMLElement elementWithName:@"body"]; 7 [body setStringValue:message]; 8 NSXMLElement *message1 = [NSXMLElement elementWithName:@"message"]; 9 [message1 addAttributeWithName:@"type" stringValue:@"chat"];10 NSString *to = [NSString stringWithFormat:@"%@@helloworldtekimac-mini.local", user];11 [message1 addAttributeWithName:@"to" stringValue:to];12 [message1 addChild:body];13 [self.xmppStream sendElement:message1];14 }
XMPP development tutorial (5)-send and receive messages