如何使用XMPPFramework发送和接收消息

Lak*_*hmi 4 xmpp objective-c ios xmppframework

我在iPhone中使用XMPP Framework创建了一个聊天应用程序.我想知道发送和接收消息的过程.任何人都可以给我一个解决方案吗?

提前致谢.

Ven*_*kat 10

下载XMPPFramework并解压缩.里面有几个文件夹.打开'Xcode'文件夹>打开'iPhoneXMPP'文件夹>点击'iPhoneXMPP.xcodeproj'>运行它.它首先要求登录凭证.成功登录后,它将显示您的好友列表.它适用于Gmail.每个传入消息都有一个回调方法:

- (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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

对于发送消息,我们必须在任何您想要的地方编写我们自己的方法:

-(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];
    }    
}
Run Code Online (Sandbox Code Playgroud)