2011-02-28 42 views
4

我正在iPhone中使用XMPP Framework创建聊天应用程序。我想知道发送和接收消息的过程。任何人都可以为我提供解决方案吗?如何使用XMPPFramework发送和接收消息

在此先感谢。

+0

发送消息的解决方案,我敢肯定,XMPP框架有文档? – Pripyat 2011-02-28 08:26:53

回答

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 
     } 
    } 
} 

要发送消息,我们必须写在任何你希望我们自己的方法:

-(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]; 
    }  
} 
1

如果从Room/Group发送消息然后使用此代码发送消息。

[xmppRoom sendMessage:@"Hi All"]; 

不需要通过xmppStream发送消息。这一行代码完全适合我。

+1

'@“Hi All”'存在'NSString'而不是'XMPPMessage'。不支持输入到sendMessage方法。您将收到此错误:发送'NSString *'到类型为'XMPPMessage *'的参数的不兼容指针类型 – 2014-06-11 07:58:02

+1

Hi Keith OYS,对不起,我不知道您正在使用哪个XMPP库,但我确实在XMPPRoom类下进行了交叉检查。它的 - (void)sendMessage:(NSString *)msg; 我也使用过。让我知道如果我错了。 – 2014-06-24 08:36:15

+1

最新的XMPPFramework使用' - (void)sendMessage:(XMPPMessage *)消息;'在'XMPPRoom'类中。因此,您需要首先初始化一个“XMPPMessage”。 – 2014-06-24 08:59:22

2

对于组发送消息/房间下面是摘录

XMPPMessage *message = [XMPPMessage message]; 
[message addBody:@"123"]; 
[self.currentRoom sendMessage:message1]; 

Where self.currentRoom is XMPPRoom 
0

下面是通过XMPPFramework斯威夫特3

let user = XMPPJID(string: "[email protected]") 
let msg = XMPPMessage(type: "chat", to: user) 
msg?.addBody("Message to send") 
self.xmppStream.send(msg)