未调用XMPP流代表?斯威夫特3

时间:2017-03-17 17:35:41

标签: swift swift3 xmpp xmppframework

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

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")
    }
   }
}

3 个答案:

答案 0 :(得分:0)

我认为你的委托方法不对。您可以尝试使用下面给出的委托方法:

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

答案 1 :(得分:0)

试试这个

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)

答案 2 :(得分:0)

我有同样的问题。在我的情况下(我按照一些教程),对象不是全局的,委托变为零。这就是它没有被调用的原因。您必须存储全局实现XMPPStreamDelegate的对象。