XMPP流委托没有被调用? Swift 3

问题描述:

我试图连接到我的iOS应用程序中的XMPP服务器。我正在使用XMPPFrameworks,并且出于某种原因,在尝试连接到服务器后,XMPP流代理没有被调用。我在我的电脑上使用第三方XMPP应用程序进行了双重检查登录信息,所以我不相信那是。我没有正确设置这个代表吗?我使用错误的语法吗?我是否需要在应用程序委托中设置它,而不是我的视图控制器?任何帮助将非常感激。以下是我的代码XMPP流委托没有被调用? Swift 3

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

#Tyler Pereira你有什么解决方案,我也面临同样的问题。 – Manish

+0

@泰勒佩雷拉,我也得到同样的问题。 – Bucket

+0

不,我还没有想出一个解决方案。我最终提出了自己的逻辑来创建一个不使用XMPP的聊天系统。 –

我认为你的委托方法是不正确的。你可以用下面给出的委托方法试试:

@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) 
+0

我在这里找到了这个解决方案[link](https://github.com/inaka/ios-xmpp-sample) –

我有同样的问题。在我的情况下(正如我遵循一些教程),该对象不是全局的,代表变为零。这就是为什么它没有被调用。您必须全局存储实现XMPPStreamDelegate的对象。