我在网上搜索了很多但是找不到可以帮助我开始实施谷歌聊天的实际示例源代码,xmpp框架提供的示例代码也没有清楚地说明它,因为它有一个Mac桌面应用程序的示例项目。
我已经能够通过xmppframework中提供的示例项目(iphoneXmpp)帮助显示所有在线/在线/离开的朋友,但它也可以告诉我们如何发起聊天。
请提供任何示例源代码,以便我可以在我的应用中初始化Google聊天。
我真的被卡住了。
提前致谢
答案 0 :(得分:2)
这是在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不会收到消息,但任何其他人收到消息..
干杯!!
答案 1 :(得分: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。
答案 2 :(得分:1)