QuickBlox Chat not logging in

656 views Asked by At

I'm working on swift and quickblox and I'm trying to have chatting occur between users. The user authentication and sign in is working its just that the chat isn't Logging in for some reason Code in question:

                QBRequest.createSessionWithExtendedParameters(parameters, successBlock: { (response : QBResponse! ,session : QBASession!) -> Void in

                var currentUser = QBUUser()
                currentUser.ID = session.userID
                currentUser.password = userPassword as String
                QBChat().addDelegate(self)
                QBChat().loginWithUser(currentUser)



                let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
                appDelegate.initiateLocationServicesUpdates()

                self.boxView.removeFromSuperview()

                self.performSegueWithIdentifier("alreadySignedInSegue", sender: self)


                }, errorBlock: { (response : QBResponse!) -> Void in
                    self.boxView.removeFromSuperview()
                    NSLog("error: %@", response.error);
                    self.view.userInteractionEnabled = true
                    var alert : UIAlertController = UIAlertController()
                    let action : UIAlertAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
                    if let error = response.error.reasons{
                        if  response.error.reasons.description.rangeOfString("Unauthorized") != nil{
                            alert = UIAlertController(title: "Oops", message: "Wrong Username/Password Combination", preferredStyle: UIAlertControllerStyle.Alert)
                            alert.addAction(action)
                            self.presentViewController(alert, animated: true, completion: nil)
                        }
                    }
                    else{
                        alert = UIAlertController(title: "Oops", message: "Something Went Wrong, Its Our Fault!", preferredStyle: UIAlertControllerStyle.Alert)
                        alert.addAction(action)
                        self.presentViewController(alert, animated: true, completion: nil)
                    }
            })

the segue found in the success block works but the value of QBChat().isLoggedIn() is always false and if I try to send a message to a user id via the

QBChat().sendMessage(message: QBChatMessage!)

function I end up getting a "Must me logged in to chat to be able to send" message . It must be a small problem that's due to me overlooking something.

edit: just so you know this is my first time working with quickblox, so please be precise about what I'm doing wrong

2

There are 2 answers

0
glebus On BEST ANSWER

You should use a shared instance of QBChat for working with chat.

QBChat.instance().addDelegate(self)
QBChat.instance().loginWithUser(currentUser)

Also check

setAutoCreateSessionEnabled

method in QBConnection. You can forget about session management.

4
9to5ios On

Please check my code working on Objective-C language.

For more info kindly check

// Login to QuickBlox Chat
[[ChatService instance] loginWithUser:[LocalStorageService shared].currentUser completionBlock:^{
    NSLog(@"------------local sotrage Logged In user=%@", [LocalStorageService shared].currentUser);
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"You have successfully logged in"
                                                                message:nil
                                                               delegate:nil
                                                      cancelButtonTitle:@"Ok"
                                                      otherButtonTitles: nil];
    [alert show];
    //
    // hide alert after delay
    double delayInSeconds = 2.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [alert dismissWithClickedButtonIndex:0 animated:YES];
    });



    [[UserChoice sharedUserChoice]setIsConnectedUser:1];
    MySlideViewController *slideViewController = [[MySlideViewController alloc] initWithNibName:@"SlideViewController" bundle:nil];
    slideViewController.myDataUser = resultG;
    slideViewController.delegate = slideViewController;
    [[UserChoice sharedUserChoice] setMyUserProfile:resultG];
    slideViewController.myDataUserIn = [[UserChoice sharedUserChoice]myUserProfile];
    slideViewController.paramGeolocalisation = paramGeo;
    [self.navigationController pushViewController:slideViewController animated:YES];
    self.navigationController.navigationBarHidden = YES;

}];

or Do this

// login to Chat
[[QBChat instance] loginWithUser:currentUser];

#pragma mark -
#pragma mark QBChatDelegate

// Chat delegate
-(void) chatDidLogin{
    // You have successfully signed in to QuickBlox Chat

    // Now you can send a message

}