我如何开始使用 Quickblox API 和 Swift?

How can I get started with Quickblox API and Swift?

我正在尝试使用 quickblox 和 swift 开发一个非常简单的聊天应用程序。

知道网上很少有教程试图解释这个过程,我开始在应用程序仪表板中创建一个用户并获取其凭据以启动连接。 (我确定用户凭据正确并且仪表板设置正确,因为我已按照此 tutorial

这是应用程序视图控制器:

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

                    }
                }

            }

        }

下面是我收到的错误,提示我没有成功连接。

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

首先,您必须使用用户登录。

此后,您可以连接聊天,最好使用 ServicesManager class 自动管理会话。

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