Search code examples
iosswiftchatquickbloxmbaas

chat login not working?(quickblox)


I have been using the same code to login the user to chat

  var user = QBUUser()   
  user.ID = session.userID    
   user.login = "123456"
   user.password = "password" 

 QBChat.instance().loginWithUser(user)

But for the last 5 days i get an error that states that the user has to login to chat.

2015-08-11 12:13:48.690 buyzar[3063:73416] -[QBChat(Deprecated) sendMessage:] -> return. You have to be logged in in order to use Chat API

Is there any change i am not aware of?


Solution

  • Make sure that you have a valid session first. Here is the code that I am using and it works perfectly as you use the completion handle to ensure that the user is logged in before you do anything else.

    //MARK: - Completions
    var logincompletion: ((success: Bool) -> ())? 
    //MARK: - Login
    
    func loginUser(login: String, password: String, completion:
        ((success: Bool) -> ())? = nil) {
            self.logincompletion = completion
            var parameters: QBSessionParameters = QBSessionParameters()
    
            parameters.userEmail = login
            parameters.userPassword = password
    
            QBRequest.createSessionWithExtendedParameters(parameters, successBlock: { (response: QBResponse!, session: QBASession!) -> Void in
    
                var currentUser = QBUUser()
                currentUser.ID = session.userID
                currentUser.password = password
                currentUser.login = login 
    
                QBChat.instance().addDelegate(self)
                QBChat.instance().loginWithUser(currentUser)
    
                }) { (response: QBResponse!) -> Void in
                    if self.logincompletion != nil {
                        self.logincompletion!(success: false)
                    }
            }
    }
    

    Here is an example of me calling this code:

    ChatManager.SharedInstance.loginUser(UserManager.SharedInstance.user!.email, password: UserManager.SharedInstance.user!.account_id) { (success) -> () in   
            println("Logged into QuickBlox: \(success)")
            if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
                if appDelegate.deviceTokenData != nil {
                    var deviceIdentifier = UIDevice.currentDevice().identifierForVendor.UUIDString
                    QBRequest.registerSubscriptionForDeviceToken(appDelegate.deviceTokenData, uniqueDeviceIdentifier: deviceIdentifier, successBlock: { (response: QBResponse!, session: [AnyObject]!) -> Void in
                        println("registered for push")
                        }) { (error: QBError!) -> Void in
                            println("could not reigster for push: \(error)")
                    }
                }
            }
        }