我正在使用XMPPFramework。当我锁定iPhone屏幕时,我的XMPP连接始终关闭。 我解锁屏幕时需要重新连接到Openfire服务器。
以下是我使用XMPPReconnect
(在Swift中)的方式:
func xmppStreamDidAuthenticate(sender: XMPPStream) {
let xmppReconnect = XMPPReconnect()
xmppReconnect.activate(sender)
xmppReconnect.addDelegate(self, delegateQueue: dispatch_get_main_queue())
}
然而,当我解锁屏幕时,它似乎永远不会重新连接。
我正确使用XMPPReconnect
吗?
我如何实现目标?
答案 0 :(得分:3)
您必须在applicationDidBecomeActive中的appdelegate中编写代码。因为当你解锁屏幕时这个方法会调用,并且在这个方法中你必须调用connect方法来打开openfire ....
func applicationDidBecomeActive(application: UIApplication) {
self.connect()
}
func connect() -> Bool {
println("connecting")
setupStream()
var jabberID: String? = "YOUR_JID"
var myPassword: String? = "YOUR_PASSWORD"
var server: String? = "YOUR_HOST"
xmppStream!.hostName = "YOURHOST_NAME"
xmppStream!.hostPort = 5222
if let stream = xmppStream {
if !stream.isDisconnected() {
return true
}
if jabberID == nil || myPassword == nil {
println("no jabberID set:" + "\(jabberID)")
println("no password set:" + "\(myPassword)")
return false
}
stream.myJID = XMPPJID.jidWithString(jabberID)
password = myPassword
var error: NSError?
if !stream.connectWithTimeout(XMPPStreamTimeoutNone, error: &error) {
var alertView: UIAlertView? = UIAlertView(title:"Error", message: "Cannot connect to \(error!.localizedDescription)", delegate: nil, cancelButtonTitle: "Ok")
alertView!.show()
return false
}
}
return true
}
希望这会有所帮助!