如何开始使用Quickblox API和Swift?

时间:2017-07-11 11:32:39

标签: ios swift quickblox

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

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

这是Application视图控制器:

        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

1 个答案:

答案 0 :(得分:1)

首先,您必须以用户身份登录。

在此之后,您可以使用聊天进行连接,最好使用自动管理会话的ServicesManager课程。

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