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");
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]; } }
source share