如何在 iPhone 应用中实现谷歌聊天
Posted
技术标签:
【中文标题】如何在 iPhone 应用中实现谷歌聊天【英文标题】:how to implement google chat in iphone app 【发布时间】:2011-03-16 13:08:42 【问题描述】:我在网上搜索了很多,但找不到可以帮助我开始 google chat 实现的实际示例源代码,xmpp 框架提供的示例代码也没有清楚地说明它,因为它有一个 Mac 桌面应用程序的示例项目。
借助 xmppframework 中提供的示例项目 (iphoneXmpp),我能够向所有在线/离线/离开的朋友展示,但它也没有说明如何发起聊天。
请提供任何示例源代码,以便我可以在我的应用中初始化谷歌聊天。
我真的卡住了。
提前致谢
【问题讨论】:
【参考方案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】:本教程应该可以解决问题:http://mobile.tutsplus.com/tutorials/iphone/building-a-jabber-client-for-ios-server-setup/
【讨论】:
以上是关于如何在 iPhone 应用中实现谷歌聊天的主要内容,如果未能解决你的问题,请参考以下文章
在网站中实现谷歌地图时自动设置搜索词(javascript)