【问题标题】:how to implement google chat in iphone app如何在 iPhone 应用中实现谷歌聊天
【发布时间】:2011-03-16 13:08:42
【问题描述】:

我在网上搜索了很多,但找不到可以帮助我开始 google chat 实现的实际示例源代码,xmpp 框架提供的示例代码也没有清楚地说明它,因为它有一个 Mac 桌面应用程序的示例项目。

借助 xmppframework 中提供的示例项目 (iphoneXmpp),我能够向所有在线/离线/离开的朋友展示,但它也没有说明如何发起聊天。

请提供任何示例源代码,以便我可以在我的应用中初始化谷歌聊天。

我真的卡住了。

提前致谢

【问题讨论】:

    标签: objective-c ios4 xmpp xmppframework


    【解决方案1】:

    好吧,在查看 xmpp 框架的桌面应用程序并尝试将其包含在我的 iphone 应用程序中后,我没有放弃并有一些解决方案..

    这是在 gmail 上向我们的聊天朋友发送消息的代码..

    -(void)sendMessage
    {
    messageStr = [NSString stringWithFormat:@"%@",[msgField text] ];
    //messageStr = [NSString stringWithString:@"hello ji....."];
    
    BOOL isEmpty = [ self validateIsEmpty:msgField.text];
    if([messageStr length] > 0 && isEmpty == NO )
    {
        NSXMLElementK *body = [NSXMLElementK elementWithName:@"body"];
        [body setStringValue:messageStr];
    
        NSXMLElementK *message = [NSXMLElementK elementWithName:@"message"];
        [message addAttributeWithName:@"type" stringValue:@"chat"];
        [message addAttributeWithName:@"to" stringValue:[[user jid] full]];
        [message addChild:body];
                [[self xmppStream ] sendElement:message];
    }
    

    在 didReceiveMessage 中,我有以下代码...

    - (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
    {
    
    NSLog(@"---------- xmppStream:didReceiveMessage: ----------");
    
    NSLog(@"--jid---%@", [[user jid] full]);
    NSLog(@"--from----%@", [message from]);
    //if(![[[user jid] full] isEqual:[message from]]) return;// important when chatting with 2 or more .. and receiving 2 or more messages...
    
    if([message isChatMessageWithBody])
    {
        NSString *msg = [[message elementForName:@"body"] stringValue];
    
        NSLog(@"mmmmmmmmmmssssssgggg-%@",msg);
    
        [str appendString:[NSString stringWithFormat:@"%@:%@\n\n", [message from], msg]];
        [chatBox setText:str];
    }
    }
    

    我可以使用这两种方法发送/接收聊天,但问题是有时我从可用在线联系人(我们可以与之聊天的人)的表格视图中选择的人的 ID 没有收到消息,但任何其他人收到消息..

    干杯!!

    【讨论】:

      【解决方案2】:
      - (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message {
      
          NSString *msg = [[message elementForName:@"body"] stringValue];
          NSString *from = [[message attributeForName:@"from"] stringValue];
      
          if (msg.length==0) {
              UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Receiving Message" 
                                                                  message:[NSString stringWithFormat:@"From %@",from]  
                                                                 delegate:nil 
                                                        cancelButtonTitle:@"Ok" 
                                                        otherButtonTitles:nil];
              [alertView show];
      
      
          }
      
          if (msg.length!=0) {
              NSMutableDictionary *m = [[NSMutableDictionary alloc] init];
              [m setObject:msg forKey:@"msg"];
              [m setObject:from forKey:@"sender"];
      
              NSLog(@"message received : %@", m);
              [_messageDelegate newMessageReceived:m];
          }
      }
      

      这对你很有用,它还会提醒你谁正在发送消息以及谁想与你聊天,但是我只是卡住了我应该从哪里为我登录的用户实现注销进入 iOS SDK。

      【讨论】:

        【解决方案3】:
        猜你喜欢
        • 2011-09-06
        • 1970-01-01
        • 1970-01-01
        • 2011-06-01
        • 1970-01-01
        • 2012-07-25
        • 1970-01-01
        • 2021-08-05
        • 1970-01-01
        相关资源
        最近更新 更多