如何在iPhone应用程序中实现谷歌聊天

发布于 2024-10-22 13:55:47 字数 252 浏览 0 评论 0原文

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

借助 xmpp 框架中提供的示例项目(iphoneXmpp),我已经能够向所有在线/离线/离开的朋友展示,但它也没有告诉任何有关如何发起聊天的信息。

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

我真的被困住了。

提前致谢

i have searched a lot on the web but can not find the actual sample source code that can help me to get started for google chat implementation , the sample code provided with the xmpp framework also does not tell clearly about it, as it have a sample project of Mac desktop application.

I have been able to show all my friends who are online/ofline/away with the help of sample project(iphoneXmpp) which is provided in the xmppframework, but it also doest tell anything about how to initiate a chat.

Please provide me any sample source code so that i can initialize the google chat in my app.

i am really stuck.

thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七分※倦醒 2024-10-29 13:55:47

好吧,我没有放弃,在研究了 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 不收到消息,但任何其他人收到消息..

干杯!

okey i didnt give up and had some solution after looking into the desktop application of xmpp framework and tried to include it in my iphone app..

here is the code to send message to our chat friend on 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];
}

and in didReceiveMessage , i have following code...

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

i'm able to send/recieve the chat using these two methods but problem is that some times the person's id which i selected from the table view of available online contacts(to whom we can chat with) does'nt receive the message but any other person receives the message..

Cheers!!

⊕婉儿 2024-10-29 13:55:47
- (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。

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

This works great for you, and it will also give you the alert who is sending the message and who wants to chat with you, However I'm just stuck from where should I implement the Logout for the user through which I logged in to iOS SDK.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文