How to send and receive messages using XMPPFramework

I am creating a chat application using the XMPP Framework on iPhone. I want to know the process of sending and receiving messages. Can someone give me a solution for this?

Thanks in advance.

+4
source share
5 answers

A quick Google search reveals many XMPP libraries , either C / C ++ or ObjC. Perhaps http://code.google.com/p/xmppframework/ would be a good starting point, although I have not tried it personally.

0
source

Download XMPPFramework and unzip it. There are several folders inside. Open the "Xcode" folder> open the "iPhoneXMPP" folder> click on "iPhoneXMPP.xcodeproj"> run it. Requests credentials first. After successful login, it will display a list of friends. It works great for gmail. There is one callback method that is called for each incoming message:

- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message { user = [xmppRosterStorage userForJID:[message from] xmppStream:sender managedObjectContext:[self managedObjectContext_roster]]; if ([message isChatMessageWithBody]) { NSString *body = [[message elementForName:@"body"] stringValue]; NSString *from = [[message attributeForName:@"from"] stringValue]; NSMutableDictionary *m = [[NSMutableDictionary alloc] init]; [m setObject:body forKey:@"msg"]; [m setObject:from forKey:@"sender"]; if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive) { NSLog(@"Applications are in active state"); //send the above dictionary where ever you want } else { NSLog(@"Applications are in Inactive state"); UILocalNotification *localNotification = [[UILocalNotification alloc] init]; localNotification.alertAction = @"Ok"; localNotification.applicationIconBadgeNumber=count; localNotification.alertBody =[NSString stringWithFormat:@"From:"%@\n\n%@",from,body]; [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification]; //send the above dictionary where ever you want } } } 

To send a message, we must write our own method whenever you want:

 -(void)sendMessage { NSString *messageStr =messageField.text; if([messageStr length] > 0) { NSLog(@"Message sending fron Gmail"); NSXMLElement *body = [NSXMLElement elementWithName:@"body"]; [body setStringValue:messageStr]; NSXMLElement *message = [NSXMLElement elementWithName:@"message"]; [message addAttributeWithName:@"type" stringValue:@"chat"]; [message addAttributeWithName:@"to" stringValue:@"destination address"]; [message addChild:body]; NSLog(@"message1%@",message); [[self appDelegate].xmppSream sendElement:message]; } } 
+10
source

To send a message in groups / Number below is a fragment

 XMPPMessage *message = [XMPPMessage message]; [message addBody:@"123"]; [self.currentRoom sendMessage:message1]; Where self.currentRoom is XMPPRoom 
+2
source

If you are sending a message from Room/Group , use this code to send messages.

 [xmppRoom sendMessage:@"Hi All"]; 

No need to send messages through xmppStream . This single line of code works fine for me.

+1
source

Here is a solution for sending a message through XMPPFramework in Swift 3

 let user = XMPPJID(string: " user@jabjab.de ") let msg = XMPPMessage(type: "chat", to: user) msg?.addBody("Message to send") self.xmppStream.send(msg) 
0
source

Source: https://habr.com/ru/post/1341597/


All Articles