Search code examples
iosswiftxmppframework

xmppStreamDidConnect never gets called in Swift


I am trying to connect to my chat server in a Swift app using XMPPFramework, but the didConnect delegate method never gets called. I have created a basic app in Objective C and I can connect and authenticate in my chat sever without problems.

In the Swift project I tried to connect with the code:

class AppDelegate: UIResponder, UIApplicationDelegate {
    var stream:XMPPStream = XMPPStream()
    var reconnect:XMPPReconnect = XMPPReconnect()
    var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    stream.addDelegate(self, delegateQueue: DispatchQueue.main)
    stream.myJID = XMPPJID(string: "[email protected]")
    reconnect.activate(stream)
    do {
        try stream.connect(withTimeout: XMPPStreamTimeoutNone)
    }
    catch let err{
        print("error occured in connecting\(String(describing: err.localizedDescription))")
    }
    return true
}

I’ve debugged XMPPFramework and in the method - (void)handleStreamFeatures a call to the delegate is executed :

[multicastDelegate xmppStreamDidConnect:self];

I’ve watched the multicastDelegateObject and has a node with reference to my delegate, and to OS_dispatch_queue_main, but after execution my xmppStreamDidConnect method isn’t executed.


Solution

  • As it is described in this Github Issue, the problem was the method declaration. My xmppStreamDidConnect need an underscore, the root problem was that if you don't import the swift extensions the compiler marks that declaration as incorrect, although it works. So to fix my problem, I need to import the pod 'XMPPFramework/Swift' and change the method declaration to

    func xmppStreamDidConnect(_ sender: XMPPStream) {