Quickblox error: You are not connected and not authorized - ios

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.

- (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

[[QBChat instance] connectWithUser:chatuserobj completion:^(NSError * _Nullable error)
{
NSLog(#"USer is Connected %#",error.description);
}];
This method is used to connect yourself to the chat and not your opponents. Furthermore, in order to connect with this method your QBUUser instance must have valid password set as password property.
Basically you need to connect yourself to the chat and then just start creating dialogs and sending messages.

Related

What is the recommended Process for using QuickBlox in iOS Share Extension

I am using QuickBlox successfully in a a main iOS app and would like to extend the features in to a Share Extension using the cocoapod 'QuickBlox', '~> 2.7.5'. I needs to send the message in the background without opening the main application.
I am using the code below without any success to first setup sending text only.
[QBRequest logInWithUserLogin:USERNAME password:PASSWORD successBlock:^(QBResponse *response, QBUUser *user) {
if (user) {
user.login = USERNAME;
user.password = PASSWORD;
[[QBChat instance] connectWithUser:user completion:^(NSError * _Nullable error) {
QBChatDialog *chatDialog = [[QBChatDialog alloc] initWithDialogID:[settingsDict valueForKey:#"sendingID"] type:QBChatDialogTypeGroup];
QBChatMessage *messagetosend = [QBChatMessage message];
messagetosend.senderID = userQBID;
messagetosend.text = self.contentText;
messagetosend.dateSent = [NSDate dateWithTimeInterval:-12.0f sinceDate:[NSDate date]];
[chatDialog joinWithCompletionBlock:^(NSError * _Nullable error) {
[chatDialog sendMessage:messagetosend completionBlock:^(NSError * _Nullable error) {
NSLog(#"%#",[error localizedDescription]);
}];
}];
}
];
}
} errorBlock:^(QBResponse * _Nonnull response) { }];
You can send the message via REST. Sending via REST doesn't need to connect to chat and join in the dialog.
NSUInteger senderID = //Current User ID
QBChatMessage *message = [QBChatMessage message];
message.text = intent.content;
message.senderID = senderID;
message.markable = YES;
message.deliveredIDs = #[#(senderID)];
message.readIDs = #[#(senderID)];
message.dialogID = dialogID;
message.dateSent = [NSDate date];
[QBRequest sendMessage:message successBlock:^(QBResponse * _Nonnull response, QBChatMessage * _Nonnull createdMessage) {
} errorBlock:^(QBResponse * _Nonnull response) {
}];

How to call a method once the completion block finish?

I want to call the signUp method first, once I got the userID, I need to call the another method normalSignupMethod.
[ConnectionObj signUp:user];
[helper normalSignupMethod:dict];
signUp Method:
[MYRequest signUp:user successBlock:^(QBResponse *response, QBUUser *user) {
// Sign up was successful
// Store user id
[SingletonClass sharedMySingleton].userID = [NSString stringWithFormat:#"%#",response.data[#"id"]];
} errorBlock:^(QBResponse *response) {
// Handle error here
NSLog(#" error in creating session %#", response.error);
[SVProgressHUD showErrorWithStatus:NSLocalizedString(#"SignUp to Chat error!", nil)];
}];
This I how I have called:
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ {
NSLog(#"Block1");
[ConnectionObj signUp:user];
});
dispatch_group_notify(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ {
NSLog(#"Group notify");
[helper normalSignupMethod:dict];
dispatch_async(dispatch_get_main_queue(), ^{
[SVProgressHUD dismiss];
});
});
Block 1 executed first, and then group notify called. But I'm getting the userID after the normalSignupMethod is finished. How to wait for a signUp method to get userID before calling the normalSignupMethod?
You can create a block with your signUp method like this and pass the Bool completion value to check is it called successfully or not. So change your method declaration like this.
-(void)signUp:(QBUser*)user andHandler:(void (^)(BOOL result))completionHandler;
And its definition
-(void)signUp:(QBUser*)user andHandler:(void (^)(BOOL result))completionHandler {
[MYRequest signUp:user successBlock:^(QBResponse *response, QBUUser *user) {
[SingletonClass sharedMySingleton].userID = [NSString stringWithFormat:#"%#",response.data[#"id"]];
completionHandler(YES);
} errorBlock:^(QBResponse *response) {
// Handle error here
NSLog(#" error in creating session %#", response.error);
[SVProgressHUD showErrorWithStatus:NSLocalizedString(#"SignUp to Chat error!", nil)];
completionHandler(NO);
}];
}
Now call this method like this.
[ConnectionObj signUp:user andHandler:^(BOOL result) {
if(result) {
[helper normalSignupMethod:dict];
}
}];
You can call the normalSignupMethod once the signUp:successBlock request returns to successBlock
[MYRequest signUp:user successBlock:^(QBResponse *response, QBUUser *user) {
// Sign up was successful
// Store user id
[SingletonClass sharedMySingleton].userID = [NSString stringWithFormat:#"%#",response.data[#"id"]];
//call the signup method
[helper normalSignupMethod:dict];
} errorBlock:^(QBResponse *response) {
// Handle error here
NSLog(#" error in creating session %#", response.error);
[SVProgressHUD showErrorWithStatus:NSLocalizedString(#"SignUp to Chat error!", nil)];
}];

send message using quickblox

I've integrated quickblox in my app. as I fetched all user in tableview using this code
QBGeneralResponsePage *page = [QBGeneralResponsePage responsePageWithCurrentPage:1 perPage:100];
[QBRequest usersForPage:page successBlock:^(QBResponse *response, QBGeneralResponsePage *pageInformation, NSArray *users)
{
[_NameArray addObjectsFromArray:users];
}
errorBlock:^(QBResponse *response)
{
}];
}
errorBlock:^(QBResponse *response)
{
NSLog(#"error: %#", response.error);
}];
in _nameArray I've all user information in QBUUSER object form
QBUUser *obj = [Array objectAtIndex:indexPath.row];
NSString *name = obj.fullname;
in retrieve all user. now when loginUser click on particular contact or retrieve user then i create private group one to one communication using this code
-(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 main thing send and receive message in that view controller i have taken textfield for sending message and table view for show message for sending message i have used this code
-(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(#"Message sent");
}];
}
and used below delegate method
- (void)chatDidReceiveMessage:(QBChatMessage *)message
I actually see the private group in admin panel of quickblox but don't see the sent message. please help me.
Instead of you're code like:
[chatDialog sendMessage:message completionBlock:^(NSError * _Nullable error)
{
NSLog(#"Message sent");
}];
Use the following code:
[QBRequest createMessage:message successBlock:^(QBResponse *response, QBChatMessage *createdMessage) {
NSLog(#"success: %#", createdMessage);
} errorBlock:^(QBResponse *response) {
NSLog(#"ERROR: %#", response.error);
}];
I use Code like:
#pragma mark Tool bar Actions
- (void)didPressSendButton:(UIButton *)button
withMessageText:(NSString *)text
senderId:(NSUInteger)senderId
senderDisplayName:(NSString *)senderDisplayName
date:(NSDate *)date {
[[QBChat instance] addDelegate:self];
QBChatMessage *message = [QBChatMessage message];
[message setText:text];
message.senderID = senderId;
message.recipientID= [[NSUserDefaults standardUserDefaults] integerForKey:#"CurrentRecipientID"];
message.dateSent = [NSDate date];
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[#"save_to_history"] = #YES;
[message setCustomParameters:params];
[QBRequest createMessage:message successBlock:^(QBResponse *response, QBChatMessage *createdMessage) {
NSLog(#"success: %#", createdMessage);
[self.chatSectionManager addMessage:createdMessage];
} errorBlock:^(QBResponse *response) {
NSLog(#"ERROR: %#", response.error);
}];
[self finishSendingMessageAnimated:YES];
}

Does HKObserverQuery can receive notification when the application not running(Killed)?

My requirement is to register for any one of the Health data like steps, weight, heart rate, etc., for background delivery using enableBackgroundDeliveryForType: method. And then create a Observer query for the same Health data to check.
In this scenario, if my application is killed by the user forcely and did changes to Health data using Health app. By this stage is it possible to get the notification to My application?
Is it mandatory to register for any of the background modes, to get notified for health data modifications through HKObserverQuery?
EDIT 1:
- (void)requestAuthorizationToShareTypes:(NSSet *)typesToShare readTypes:(NSSet *)typesToRead completion:(void (^)(BOOL, NSError *))completion
{
if ([HKHealthStore isHealthDataAvailable]) {
[self.healthStore requestAuthorizationToShareTypes:typesToShare readTypes:typesToRead completion:^(BOOL success, NSError *error) {
if(success)
{
self.authorizationSuccess = YES;
completion(YES, nil);
}
else
{
[MyUtilities showAlertWithTitle:#"Authorization fail!" message:#"You didn't allow to access Health data."];
completion(NO, error);
return;
}
}];
}
else
{
[MyUtilities showAlertWithTitle:#"Sorry!" message:#"Your device does not support Health data."];
NSDictionary *errorDictionary = #{ NSLocalizedDescriptionKey : #"Your device doesn't support Health Kit."};
NSError *error = [[NSError alloc] initWithDomain:#"" code:2 userInfo:errorDictionary];
completion(NO, error);
return;
}
}
- (void)authorizeConsumeHealth:(void (^)(BOOL, NSError *))completion
{
NSSet *readObjectTypes = [NSSet setWithObjects:
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCaffeine],
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCalcium],
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryChloride],
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryIron],
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount],
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodPressureSystolic],
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodPressureDiastolic], [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount],
nil];
[self requestAuthorizationToShareTypes:nil readTypes:readObjectTypes completion:^(BOOL success, NSError *error) {
if(completion)
{
completion(success, error);
[self observeStepCountChanges];
}
}];
}
- (void)observeStepCountChanges
{
HKSampleType *sampleType = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
[self.healthStore enableBackgroundDeliveryForType:sampleType frequency:HKUpdateFrequencyImmediate withCompletion:^(BOOL success, NSError *error) {}];
HKObserverQuery *query = [[HKObserverQuery alloc] initWithSampleType:sampleType predicate:nil updateHandler:^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError *error) {
if(error)
{
NSLog(#"Steps count observer completion block. Error: %#", error);
}
else
{
NSLog(#"Steps count observer completion block");
}
}];
[self.healthStore executeQuery:query];
}
Please help me. Thanks.

FBSession Must Be Specified Parse

I am trying to follow the Parse tutorial for Logging in With Facebook. However, the sample code does not match up with the guide, so the code there is useless. I have followed the guide completely, but after I login, it directs me to Facebook app, I give permission, it goes back to the app I am building, but I get the following error
FBSDKLog: Error for request to endpoint 'me': An open FBSession must be specified for calls to this endpoint.
What is going on? In Login controller:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
FBRequest *request = [FBRequest requestForMe];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// handle successful response
} else if ([[[[error userInfo] objectForKey:#"error"] objectForKey:#"type"]
isEqualToString: #"OAuthException"]) { // Since the request failed, we can check if it was due to an invalid session
NSLog(#"The facebook session was invalidated");
[self logoutButtonAction:nil];
} else {
NSLog(#"Some other error: %#", error);
}
}];
if ([PFUser currentUser] && // Check if user is cached
[PFFacebookUtils isLinkedWithUser:[PFUser currentUser]]) { // Check if user is linked to Facebook
// Present the next view controller without animation
[self _presentUserDetailsViewControllerAnimated:NO];
}
}
- (IBAction)loginButtonTouchHandler:(id)sender {
// Set permissions required from the facebook user account
NSArray *permissionsArray = #[ #"user_about_me", #"user_relationships", #"user_birthday", #"user_location"];
[PFFacebookUtils initializeFacebook];
// Login PFUser using Facebook
[PFFacebookUtils logInWithPermissions:permissionsArray block:^(PFUser *user, NSError *error) {
[_activityIndicator stopAnimating]; // Hide loading indicator
if (!user) {
NSString *errorMessage = nil;
if (!error) {
NSLog(#"Uh oh. The user cancelled the Facebook login.");
errorMessage = #"Uh oh. The user cancelled the Facebook login.";
} else {
NSLog(#"Uh oh. An error occurred: %#", error);
errorMessage = [error localizedDescription];
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Log In Error"
message:errorMessage
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:#"Dismiss", nil];
[alert show];
} else {
if (user.isNew) {
NSLog(#"User with facebook signed up and logged in!");
} else {
NSLog(#"User with facebook logged in!");
}
[self _presentUserDetailsViewControllerAnimated:YES];
}
}];
[_activityIndicator startAnimating]; // Show loading indicator until login is finished
}
- (void)_presentUserDetailsViewControllerAnimated:(BOOL)animated {
UserDetailsViewController *detailsViewController = [[UserDetailsViewController alloc] init];
[self.navigationController pushViewController:detailsViewController animated:YES];
}
In my UserDetailsViewController:
- (void)viewDidLoad {
// ...
[self _loadData];
}
- (void)_loadData {
// ...
FBRequest *request = [FBRequest requestForMe];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// result is a dictionary with the user's Facebook data
NSDictionary *userData = (NSDictionary *)result;
NSString *facebookID = userData[#"id"];
NSString *name = userData[#"name"];
NSString *location = userData[#"location"][#"name"];
NSString *gender = userData[#"gender"];
NSString *birthday = userData[#"birthday"];
NSString *relationship = userData[#"relationship_status"];
NSURL *pictureURL = [NSURL URLWithString:[NSString stringWithFormat:#"https://graph.facebook.com/%#/picture?type=large&return_ssl_resources=1", facebookID]];
// URL should point to https://graph.facebook.com/{facebookId}/picture?type=large&return_ssl_resources=1
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:pictureURL];
// Run network request asynchronously
[NSURLConnection sendAsynchronousRequest:urlRequest
queue:[NSOperationQueue mainQueue]
completionHandler:
^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError == nil && data != nil) {
// Set the image in the header imageView
self.headerImageView.image = [UIImage imageWithData:data];
}
}];
// Now add the data to the UI elements
// ...
}
}];
}
We figured it out, when trying to create an auto-login feature with this function:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
FBRequest *request = [FBRequest requestForMe];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// handle successful response
} else if ([[[[error userInfo] objectForKey:#"error"] objectForKey:#"type"]
isEqualToString: #"OAuthException"]) { // Since the request failed, we can check if it was due to an invalid session
NSLog(#"The facebook session was invalidated");
[self logoutButtonAction:nil];
} else {
NSLog(#"Some other error: %#", error);
}
}];
if ([PFUser currentUser] && // Check if user is cached
[PFFacebookUtils isLinkedWithUser:[PFUser currentUser]]) { // Check if user is linked to Facebook
// Present the next view controller without animation
[self _presentUserDetailsViewControllerAnimated:NO];
}
}
We actually end up skipping the [PFFacebookUtils initializeFacebook] call, since it only happens when you push the login button. The solution is to put this call in the appDelegate in the method application:didFinishLaunchingWithOptions:

Resources