Search code examples
iosobjective-cquickblox

Quickblox error: You are not connected and not authorized


I know this question has been asked multiple time but none of them solved my problem.

Error Domain=com.quickblox.chat Code=401 "Password not verified" actually I have tried this:

- (void)viewDidLoad
{
    [super viewDidLoad];

    appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    NSLog(@"bdsfbd %@",chatuserobj.fullName);
    NSLog(@"Chat Id %lu",(unsigned long)chatuserobj.ID);
    NSLog(@"Current User %@",[QBSession currentSession].currentUser);

    QBUUser *currentUserr = [QBUUser user];
    currentUserr.ID = appDelegate.loginUserId;
    currentUserr.password = appDelegate.loginUserPassword;

    // connect to Chat
    [QBRequest logInWithUserLogin:appDelegate.loginUser password:appDelegate.loginUserPassword successBlock:^(QBResponse *response, QBUUser *user)
    {
        chatDialog = [[QBChatDialog alloc] initWithDialogID:NULL type:QBChatDialogTypePrivate];
        chatDialog.occupantIDs = @[@(chatuserobj.ID)];
        [QBRequest createDialog:chatDialog successBlock:^(QBResponse *response, QBChatDialog *createdDialog)
         {
             NSLog(@"Created Dialog  %@",createdDialog);
         } errorBlock:^(QBResponse *response)
         {
             NSLog(@"Error  %@",response);
         }];            
    } errorBlock:^(QBResponse *response) {

    }];

    [[QBChat instance] connectWithUser:chatuserobj completion:^(NSError * _Nullable error)
     {
         NSLog(@"USer is Connected %@",error.description);
         [self startChat];
     }];

    [QBSettings setKeepAliveInterval:30];
    [QBSettings setAutoReconnectEnabled:YES];
}

and this

-(void)startChat
{
    [[QBChat instance] addDelegate:self];
    QBChatMessage *message = [QBChatMessage message];
    [message setText:@"Hey there"];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"save_to_history"] = @YES;
    [message setCustomParameters:params];
    [chatDialog sendMessage:message completionBlock:^(NSError * _Nullable error)
    {
        NSLog(@"Completed: %@",error.description);
    }];
}

I don't know where I am wrong. So point out my mistake.

EDIT: again in did load

- (void)viewDidLoad
{
    [super viewDidLoad];


    // connect to Chat


    [[QBChat instance] connectWithUser:currentUserr completion:^(NSError * _Nullable error)
     {
         NSLog(@"USer is Connected %@",error.description);
     }];

    dispatch_async(dispatch_get_main_queue(), ^(void)
        {
            [self chat];
        });
    dispatch_async(dispatch_get_main_queue(), ^(void)
                   {
                       [self startChat];
                   });
}

chat is method for creating dialog for private group or one to one connection

-(void)chat
{
    chatDialog = [[QBChatDialog alloc] initWithDialogID:NULL type:QBChatDialogTypePrivate];

    chatDialog.occupantIDs = @[@(chatuserobj.ID)];

    [QBRequest createDialog:chatDialog successBlock:^(QBResponse *response, QBChatDialog *createdDialog) {

    } errorBlock:^(QBResponse *response) {

    }];
}

and start chat actual communication occur

-(void)startChat
{
    [[QBChat instance] addDelegate:self];
    QBChatMessage *message = [QBChatMessage message];

    [message setText:@"Hey there"];

    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"save_to_history"] = @YES;

    [message setCustomParameters:params];

    [chatDialog sendMessage:message completionBlock:^(NSError * _Nullable error)
    {
        NSLog(@"Completed: %@",error.description);

    }];
}

now this error occur UserInfo={NSLocalizedRecoverySuggestion = You are not connected to chat.


Solution

  • - (void)viewDidLoad
    {
        [super viewDidLoad];
    
    
        // connect to Chat
    
    
        [[QBChat instance] connectWithUser:currentUserr completion:^(NSError * _Nullable error)
         {
             NSLog(@"USer is Connected %@",error.description);
    
             dispatch_async(dispatch_get_main_queue(), ^(void)
    
                            {
                                 [self chat];
    
    
                            });
    
    
             dispatch_async(dispatch_get_main_queue(), ^(void)
    
                            {
                                 [self startChat];
    
    
                            });
    
         }];
    
    
    
    }
    

    and same method of start chat and chat. it works well