Search code examples
iosswiftquickblox

How can I get started with Quickblox API and Swift?


I am trying to develop a very simple chat application using quickblox and swift.

Knowing that there are few tutorials online that try to explain the process, I started with creating a user in the application dashboard and getting its credentials to initiate the connection. (I am certain that the user credential are correct and the dashboard is correctly set as I have followed this tutorial)

Here is the Application view controller:

        import UIKit
        import Quickblox

        class ViewController: UIViewController {



            override func viewDidLoad() {
                super.viewDidLoad()


                let user = QBUUser()
                user.id = 29777469
                user.password = "tahrisqalli"




                QBChat.instance().connect(with: user) { (error) in
                    if error != nil {
                        print("error: \(error)")
                    }
                    else {
                        print("login to chat succeeded")

                    }
                }

            }

        }

and following is the error I get telling me that I did not connect successfully.

2017-07-11 11:33:50.837 QuickbloxTutorial[1045:24701] [ChatService] Connecting to Chat, host: chat.quickblox.com, user JID: 29777469-0@chat.quickblox.com/DCB0A1F4-3A56-49AD-9639-8C2A6BBE7B08
    2017-07-11 11:33:52.042 QuickbloxTutorial[1045:24711] [ChatService] Stream isSecure: YES
    2017-07-11 11:33:52.658 QuickbloxTutorial[1045:24722] [ChatService] Stream did connect, supportsStartTLS: YES
    2017-07-11 11:33:52.824 QuickbloxTutorial[1045:24722] [ChatService] Did not authenticate, error: <failure xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><not-authorized/></failure>
    error: Optional(Error Domain=com.quickblox.chat Code=401 "<failure xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><not-authorized/></failure>" UserInfo={NSLocalizedDescription=<failure xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><not-authorized/></failure>})
    2017-07-11 11:33:52.842 QuickbloxTutorial[1045:24722] [ChatService] Did disconnect

Solution

  • First of all, you have to log in with a user.

    After this, You can connect with chat and it is best for you to use ServicesManager class that manage session automatically.

    let loginUser = QBUUser()
    loginUser.id = 29777469
    loginUser.password = "tahrisqalli"
    
    ServicesManager.instance().authService.logInWithUser(loginUser, completion: { (response, qbUser) in
    if qbUser != nil {
         ServicesManager.instance().chatService.connectWithCompletionBlock { (error) in
              if error != nil {
                 print("user not connected error: ",error?.description)
              } else {
                 //user connect successfully
              }
         }
         print(qbUser)
    } else {
         print(response.error?.description)
      }
    })