未调用 XMPP 流委托?斯威夫特 3.



我正在尝试连接到iOS应用程序中的XMPP服务器。我正在使用 XMPPFrameworks,由于某种原因,在我尝试连接到服务器后没有调用 XMPP 流委托。我已经在我的计算机上使用第三方XMPP应用程序仔细检查了登录信息,所以我不相信是这样。我没有正确设置此委托吗?我是否使用了错误的语法?是否需要在应用委托而不是视图控制器中设置此设置?任何帮助将不胜感激。下面是我的代码

import UIKit
import XMPPFramework
class ViewController: UIViewController, XMPPStreamDelegate {
override func viewDidLoad() {
    super.viewDidLoad()
    connect()
    // Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
func connect() {
    let stream = XMPPStream()
    stream?.addDelegate(self, delegateQueue: DispatchQueue.main)
    stream?.myJID = XMPPJID.init(string: "XXXXXXXXXXX")
    stream?.hostName = "XXXXXXXXX"
    stream?.hostPort = 5222
    do {
        try stream?.connect(withTimeout: XMPPStreamTimeoutNone)
    } catch {
        print("error connecting")
    }
}
func xmppStreamDidConnect(sender: XMPPStream) {
    print("connected!")
    do {
        try sender.authenticate(withPassword: "XXXXXXXXXX")
    } catch {
        print("error registering")
    }
   }
}
我认为

您的委托方法不正确。您可以尝试使用下面给出的委托方法:

@objc func xmppStreamDidConnect(_ sender: XMPPStream!) {
  //write your code here.
}

试试这个

do {
        try self.xmppController = XMPPController(hostName: server,
                                                 userJIDString: userJID,
                                                 password: userPassword)
        self.xmppController.xmppStream.addDelegate(self, delegateQueue: DispatchQueue.main)
        self.xmppController.connect()
    } catch {
        sender.showErrorMessage(message: "Something went wrong")
    }

XMPPController

class XMPPController: NSObject {
var xmppStream: XMPPStream
let hostName: String
let userJID: XMPPJID
let hostPort: UInt16
let password: String
init(hostName: String, userJIDString: String, hostPort: UInt16 = 5222, password: String) throws {
    guard let userJID = XMPPJID(string: userJIDString) else {
        throw XMPPControllerError.wrongUserJID
    }
    self.hostName = hostName
    self.userJID = userJID
    self.hostPort = hostPort
    self.password = password
    // Stream Configuration
    self.xmppStream = XMPPStream()
    self.xmppStream.hostName = hostName
    self.xmppStream.hostPort = hostPort
    self.xmppStream.startTLSPolicy = XMPPStreamStartTLSPolicy.allowed
    self.xmppStream.myJID = userJID
    super.init()
    self.xmppStream.addDelegate(self, delegateQueue: DispatchQueue.main)
}
func connect() {
    if !self.xmppStream.isDisconnected() {
        return
    }
    try! self.xmppStream.connect(withTimeout: XMPPStreamTimeoutNone)
}}

它对我有用。 需要你注意这条线

try self.xmppController = XMPPController(hostName: server,
                                                 userJIDString: userJID,
                                                 password: userPassword)

我遇到了同样的问题。就我而言(正如我遵循一些教程一样(,对象不是全局的,委托变为 nil。这就是为什么它没有被调用。您必须全局存储实现 XMPPStreamDelegate 的对象。

最新更新