我正在尝试在我的应用程序中实现一个Xmpp客户端。我打算在我的应用程序中使用以下代码但是代码的某些部分似乎没有执行,这阻止了我连接到服务器。 https://github.com/processone/xmpp-messenger-ios
我正在尝试使用我的jabberID和密码连接到服务器但是对于某些代码永远不会执行:
if let _ = error {
let alertController = UIAlertController(title: "Sorry", message: "An error occured: \(error)", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: { (UIAlertAction) -> Void in
//do smt
}))
self.presentViewController(alertController, animated: true, completion: nil)
} else {
self.dismissViewControllerAnimated(true, completion: nil)
}
以下是与问题相关的整个代码:
@IBAction func validate(sender: AnyObject) {
if OneChat.sharedInstance.isConnected() {
OneChat.sharedInstance.disconnect()
usernameTextField.hidden = false
passwordTextField.hidden = false
validateButton.setTitle("Validate", forState: UIControlState.Normal)
} else {
OneChat.sharedInstance.connect(username: self.usernameTextField.text!, password: self.passwordTextField.text!) {(stream, error) -> Void in
if let _ = error {
let alertController = UIAlertController(title: "Sorry", message: "An error occured: \(error)", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: { (UIAlertAction) -> Void in
//do smt
}))
self.presentViewController(alertController, animated: true, completion: nil)
} else {
self.dismissViewControllerAnimated(true, completion: nil)
}
}
}
}
public typealias OneChatConnectCompletionHandler = (stream: XMPPStream, error: DDXMLElement?) -> Void
public func connect(username username: String, password: String, completionHandler completion:OneChatConnectCompletionHandler) {
if isConnected() {
streamDidConnectCompletionBlock = completion
self.streamDidConnectCompletionBlock!(stream: self.xmppStream!, error: nil)
return
}
if (username == "kXMPPmyJID" && NSUserDefaults.standardUserDefaults().stringForKey(kXMPP.myJID) == "kXMPPmyJID") || (username == "kXMPPmyJID" && NSUserDefaults.standardUserDefaults().stringForKey(kXMPP.myJID) == nil) {
streamDidConnectCompletionBlock = completion
streamDidConnectCompletionBlock!(stream: self.xmppStream!, error: DDXMLElement(name: "Please set crendentials before trying to connect"))
return
}
if username != "kXMPPmyJID" {
setValue(username, forKey: kXMPP.myJID)
setValue(password, forKey: kXMPP.myPassword)
}
if let jid = NSUserDefaults.standardUserDefaults().stringForKey(kXMPP.myJID) {
xmppStream?.myJID = XMPPJID.jidWithString(jid)
} else {
streamDidConnectCompletionBlock = completion //was false
streamDidConnectCompletionBlock!(stream: self.xmppStream!, error: DDXMLElement(name: "Bad username"))
}
if let password = NSUserDefaults.standardUserDefaults().stringForKey(kXMPP.myPassword) {
self.password = password
} else {
streamDidConnectCompletionBlock = completion //was false
streamDidConnectCompletionBlock!(stream: self.xmppStream!, error: DDXMLElement(name: "Bad password"))
}
try! xmppStream!.connectWithTimeout(XMPPStreamTimeoutNone)
streamDidConnectCompletionBlock = completion
}
答案 0 :(得分:0)
也许,你期待永远不会发生的超时(库的配置) 我有同样的问题,最后,我说我的问题是主机名和端口。你尝试过这样的事情吗?
let usuario = "name@domain.com";
OneChat.sharedInstance.connect(username: usuario, password: pass) { (stream, error) -> Void in
if let _ = error {
let alertController = UIAlertController(title: "Sorry", message: "An error occured: \(error)", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: { (UIAlertAction) -> Void in
//do something
}))
self.presentViewController(alertController, animated: true, completion: nil)
} else {
print("I did it!")
}
}