2011-02-28 109 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)