折腾SwiftXMPP的代码时,在模拟器中发送消息
但是对方,已经登陆的Mac的Aduim客户端中,却收不到消息
不过:
客户端发送消息,代码中是可以收到的:
调试期间,发现是:
原来是:
发送消息的to的值,设置有误:
to的值,给了默认的变量ChatViewController的
var chatWithUser: String = “teste03@local”
而别处通过:
BuddyListViewController.swift
中去获得对应的chat的view controller:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { print("didSelectRowAtIndexPath") let userName: String? = onlineBuddies.objectAtIndex(indexPath.row) as? String let storyBoard = UIStoryboard(name: "Main", bundle: nil) let chatController: ChatViewController! = storyBoard.instantiateViewControllerWithIdentifier("chatViewController") as! ChatViewController print("chatController=\(chatController)") //chatController=<SwiftXMPP.ChatViewController: 0x7feab2e36380> if let controller = chatController { controller.chatWithUser = userName! print("controller=\(controller)") //controller=<SwiftXMPP.ChatViewController: 0x7feab2e36380> //presentModalViewController(controller, animated: true) //presentViewController(controller, animated: true, completion: nil) //[self presentViewController: controller animated:YES completion:nil]; } // print(chatController) }
虽然通过:
chatViewController是可以新生成对应的chat的VC的,但是此处的是:
chatController=<SwiftXMPP.ChatViewController: 0x7feab2e36380>
和真正的ChatViewController.swift中的:
print(“self=\(self)”) //<SwiftXMPP.ChatViewController: 0x7feab2f84150>
是不一样的:
即,BuddyListViewController.swift中没有设置成功对应的chatViewController中的chatWithUser
原因是:通过storyBoard.instantiateViewControllerWithIdentifier是去新生成了新的Chat的VC,而没有想办法获得已有的ChatViewController
所以设置的新的chatWithUser的值,是设置到新生成的VC中去了。。
但是未搞定,换成:
然后在BuddyListViewController.swift中就可以去更新ChatViewController中的变量chatWithUser,
然后就可以发送消息了:
messages=( { msg = "can send from SwiftXMPP"; sender = you; }, { msg = "send to swiftXMPP from Adium"; sender = "[email protected]/licrifandeMacBook-Pro"; } )
对应的xml的message分别是:
<message type="chat" to="[email protected]"><body>can send from SwiftXMPP</body></message>
和:
<message xmlns="jabber:client" type="chat" id="purple8f30ed85" to="[email protected]/4d225685" from="[email protected]/licrifandeMacBook-Pro"><body>send to swiftXMPP from Adium</body></message>
格式化后:
<message to="[email protected]" type="chat"> <body>can send from SwiftXMPP</body> </message>
和:
<message from="[email protected]/licrifandeMacBook-Pro" id="purple8f30ed85" to="[email protected]/4d225685" type="chat" xmlns="jabber:client"> <body>send to swiftXMPP from Adium</body> </message>
然后Adium就收到了新消息了:
然后,贴上最后可以工作的代码:
https://github.com/crifan/SwiftXMPP_Crifan/blob/master/SwiftXMPP_CrifanLi_20151127_sendReceiveMsgAllOk.zip
测试效果如下:
转载请注明:在路上 » [已解决]SwiftXMPP代码中发送消息失败:对方收不到消息