我在网上搜索了很多,但找不到可以帮助我开始使用 google 聊天实现的实际示例源代码,xmpp 框架提供的示例代码也没有清楚地说明它,因为它有一个示例Mac 桌面应用程序项目。
在 xmppframework 中提供的示例项目(iphoneXmpp)的帮助下,我已经能够向所有在线/离线/离开的朋友展示,但它也没有说明如何发起聊天。
请向我提供任何示例源代码,以便我可以在我的应用程序中初始化谷歌聊天。
我真的被困住了。
提前致谢
我在网上搜索了很多,但找不到可以帮助我开始使用 google 聊天实现的实际示例源代码,xmpp 框架提供的示例代码也没有清楚地说明它,因为它有一个示例Mac 桌面应用程序项目。
在 xmppframework 中提供的示例项目(iphoneXmpp)的帮助下,我已经能够向所有在线/离线/离开的朋友展示,但它也没有说明如何发起聊天。
请向我提供任何示例源代码,以便我可以在我的应用程序中初始化谷歌聊天。
我真的被困住了。
提前致谢
好吧,在查看 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 没有收到消息,但任何其他人收到消息..
干杯!!
- (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 的用户实现注销开发工具包。
本教程应该可以解决问题:http: //mobile.tutsplus.com/tutorials/iphone/building-a-jabber-client-for-ios-server-setup/