robbiehanson / XMPPFramework

An XMPP Framework in Objective-C for Mac and iOS
Other
5.91k stars 2.09k forks source link

xmppStreamDidConnect never gets called in Swift #1126

Open eduhcano opened 5 years ago

eduhcano commented 5 years ago

I am trying to connect to my chat server in a Swift app, 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: “user@chatserver.net")
    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.

gotamafandy commented 5 years ago

you have to add xmppStream delegate

xmppStream.addDelegate(self, delegateQueue: DispatchQueue.main)

eduhcano commented 5 years ago

Hi @gotamafandy , yes, I added it just after didfinishLaunchingWithOptions

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    stream.addDelegate(self, delegateQueue: DispatchQueue.main)
    ....
gotamafandy commented 5 years ago

@eduhcano sorry i didn't see that :), do you also implement XMPPStreamDelegate somewhere in your code?

eduhcano commented 5 years ago

No problem, thanks for your help :) Yes, I made AppDelegate a delegate for XMPPStreamDelegate.

In order to investigate, I have created two basic projects, one in Objective C, which connects smoothly and the other in Swift. In the Swift project, XMPP calls the delegate func xmppStream (_ sender: XMPPStream, willSecureWithSettings settings: NSMutableDictionary) (I don't know why in ObjC don't) then, in my delegates, I complete the security functions and then, debugging, I see that Xmpp Framework calls [multicastDelegate xmppStreamDidConnect:self]; However my delegate never triggers. I've attached the projects. xmpp_lab.zip

gotamafandy commented 5 years ago

@eduhcano try changing your pod into pod 'XMPPFramework/Swift'

and update all your XMPPStreamDelegate methods, e.g:

from:

func xmppStreamWillConnect(sender: XMPPStream!) {
        print("will connect")
    }

into:

func xmppStreamWillConnect(_ sender: XMPPStream) {
        print("will connect")
    }

i've updated the delegate on your code and it's working

eduhcano commented 5 years ago

YES!! now works perfect! 👯‍♂️ As I wasn't importing the Swift extensions, the compiler marked as incorrect the method declaration with the underscore. I am very grateful to you @gotamafandy, you have helped me a lot.

eduhcano commented 5 years ago

@gotamafandy I'm really sorry for abusing your kindness, but I'm facing the same problem trying to connect to a room. I've declared my delegate as:

extension XMPPRoom: XMPPRoomDelegate{
    public func xmppRoomDidJoin(_ sender: XMPPRoom) {
        print("room did join")
    }
}

Connection happens and the framework calls the delegate with [multicastDelegate xmppRoomDidJoin:self]; But my delegate implementation never triggers, I've been trying to find out since yesterday what is the correct method declaration without success.

I've made an example project again. Xmpp_swift.zip

gotamafandy commented 5 years ago

hi @eduhcano sorry for late reply, have you resolve the issue?

eduhcano commented 5 years ago

No worries @gotamafandy, you helped me a lot. The truth is that I didn't get to activate the room join event, so I made a workaround and checked the connection status when I need to know it.