How to handle sessions with QuickBlox in iOS - ios

I am using Quickblox example writing in Swift for chat app. It uses QMServices.
Questions:
1) is there any approach for user login in iOS application with some session_token generated in any cloud backend ?
2) please help - how to renew user sessions in right way, I always get 422 error (validation error) multiple times (
In app using QuickBlox for iOS I try to login user in appdelegate:
if Storage.sharedInstance.currentUser != nil {
if !ServicesManager.instance().isAuthorized() {
let user =Storage.sharedInstance.currentUser
ServicesManager.instance().loginOrSignUp(user) {
[unowned self] success, error in
if (success) {
self.openMainScreen()
}
}
} else {
openMainScreen()
}
}
And loginOrSignUp function is:
func loginOrSignUp(user: QBUUser!, completion: ((success:Bool, errorMessage: String!) -> Void)!) {
self.logInWithUser(user) { (success:Bool, errorMessage: String!) -> Void in
if (success) {
Storage.sharedInstance.currentUser = self.currentUser()
completion(success: success, errorMessage: errorMessage)
} else {
self.authService.signUpAndLoginWithUser(user, completion: { (response: QBResponse!, user: QBUUser!) -> Void in
if user != nil {
Storage.sharedInstance.currentUser = self.currentUser()
completion(success: true, errorMessage: errorMessage)
} else {
completion(success: false, errorMessage: response.error?.error?.localizedDescription)
}
})
}
}
}
For example using Parse cloud code I can call function from cloud code:
Parse.Cloud.define("signUp", function(req, res) {
var phoneNumber = req.params.phoneNumber;
Parse.Cloud.useMasterKey();
var query = new Parse.Query(Parse.User);
query.equalTo('username', phoneNumber);
query.find({
success: function(results) {
if (results.length > 0) {
Parse.User.logIn(phoneNumber, secretWord).then(function (user) {
res.success(user.getSessionToken());
}, function (err) {
res.error(err);
});
} else {
var user = new Parse.User();
user.set("username",phoneNumber);
user.set("password",secretWord);
user.setACL({"*": { "read": true }});
user.signUp(null, {
success: function(user) {
res.success(user.getSessionToken());
// Hooray! Let them use the app now.
},
error: function(user, error) {
alert("Error: " + error.code + " " + error.message);
}
});
}
},
error: function(error) {
res.error(err);
}
});
});
So using Parse I can just login to app with session_token generated with cloud code.
First question: can I do the same with QuickBlox - use for login some session_token generated in any cloud backend?
And the second question: when user logged in, session is expired in 2 hours and we have to renew session ?
When user has bad connection, before he receive useful information he has to renew session every 2 hours. Even Ukrainian API for Ukrainian usage (not for for best service in the world) use token generated with login function for any other requests. User should not renew session always - he get only useful information.
But if there is no approach in Quickblox without session renewing, then how to do this renew in right way ? I am very new to iOS development, but when I used Parse, I didi not get this problems (

Related

Firebase Email Verification Redirect Url

I incorporated Firebase's email verification for my iOS mobile app and am trying to resolve the following issues:
The length of the redirect url appears extremely long. It looks like it repeats itself.
https://app.page.link?link=https://app.firebaseapp.com//auth/action?apiKey%3XXX%26mode%3DverifyEmail%26oobCode%3XXX%26continueUrl%3Dhttps://www.app.com/?verifyemail%253Demail#gmail.com%26lang%3Den&ibi=com.app.app&ifl=https://app.firebaseapp.com//auth/action?apiKey%3XXX%26mode%3DverifyEmail%26oobCode%3XXX%26continueUrl%3Dhttps://www.app.com/?verifyemail%253Demail#gmail.com%26lang%3Den
When I set handleCodeInApp equal to true, and am redirected back to the app when I click on the redirect url, the user's email is not verified. Whereas when I set it to false and go through Firebase's provided web widget, it does get verified. Wasn't able to find documentation that outlined handling the former in swift...
Any thoughts are appreciated.
func sendActivationEmail(_ user: User) {
let actionCodeSettings = ActionCodeSettings.init()
let redirectUrl = String(format: "https://www.app.com/?verifyemail=%#", user.email!)
actionCodeSettings.handleCodeInApp = true
actionCodeSettings.url = URL(string: redirectUrl)
actionCodeSettings.setIOSBundleID("com.app.app")
Auth.auth().currentUser?.sendEmailVerification(with: actionCodeSettings) { error in
guard error == nil else {
AlertController.showAlert(self, title: "Send Error", message: error!.localizedDescription)
return
}
}
}
Make sure you're verifying the oobCode that is part of the callback URL.
Auth.auth().applyActionCode(oobCode!, completion: { (err) in
if err == nil {
// reload the current user
}
})
Once you have done that, try reloading the the user's profile from the server after verifying the email.
Auth.auth().currentUser?.reload(completion: {
(error) in
if(Auth.auth().currentUser?.isEmailVerified)! {
print("email verified")
} else {
print("email NOT verified")
}
})

Quick blox chat message sending response is success but messages are not delivered to server

Just working with Quick blox Chat API though its interesting and fun but stuck horribly when coming to sending messages successfully to group.
I first created a ChatDialog(not public group) and its created with out any problems.Now i want to send messages to the ChatDialog.
This is the code at Client side in Swift :
let user = QBUUser()
user.ID = (QBSession.currentSession().currentUser?.ID)!
user.password = "*********"
QBChat.instance().connectWithUser(user) { (error: NSError?) -> Void in
if error == nil{
print("Success in connection")
self.chatGroup = QBChatDialog(dialogID:self.selectedChatGroup.chatGroupId, type: QBChatDialogType.Group)
self.chatGroup.occupantIDs = self.selectedChatGroup.opponents
self.chatGroup.joinWithCompletionBlock { (err) in
if err == nil{
print("Joined Succesfully")
let message: QBChatMessage = QBChatMessage()
message.text = "PRAISE THE LORD"
message.deliveredIDs = [(QBSession.currentSession().currentUser?.ID)!]
message.readIDs = [(QBSession.currentSession().currentUser?.ID)!]
message.markable = true
self.chatGroup.sendMessage(message, completionBlock: { (error: NSError?) -> Void in
if err == nil{
print(message.text)
print("Message sent Succesfully")
let resPage = QBResponsePage(limit:20, skip: 0)
QBRequest.messagesWithDialogID(self.selectedChatGroup.chatGroupId, extendedRequest: nil, forPage: resPage, successBlock: {(response: QBResponse, messages: [QBChatMessage]?, responcePage: QBResponsePage?) in
print("Messages count is \(messages?.count)")
}, errorBlock: {(response: QBResponse!) in
})
}else{
print(err?.localizedDescription)
}
});
}
else{
print(err?.localizedDescription)
}
}
}
}
This is the Response that i am getting back.i am pretty sure that logging to chat, connecting to group are successful. Even from the response given back from the API is also affirming that sending message is successful.But i am not able to see any messages in the Admin Panel at Quick blox. And also tried getting all the messages from the Chat dialog but its giving 0 messages.So my messages are not getting delivered.
Yah, got the solution from Quickblox API.This is what i missed in my code.
var params = NSMutableDictionary()
params["save_to_history"] = true
message.customParameters = params
Worked like a charm!

What are the possible causes of error 14000, "Fail to start messaging", in the SendBird iOS SDK?

I'm trying to use the SendBird SDK to implement a simple instant messaging feature in my iOS app.
I've read the Quick Start, Authentication, and Messaging Channel - Basic guides, and I've attempted to set up a 1-1 messaging channel as they describe.
In application:didFinishLaunchingWithOptions I included SendBird.initAppId("MyAppID").
In my initial view controller, I log in the user. I'm using the user's username from my app as his userId, and I'm concatenating his first and last name to use as his SendBird nickname:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if let emailAddress = KCSUser.activeUser().email,
firstName = KCSUser.activeUser().givenName,
lastName = KCSUser.activeUser().surname {
let nickname = firstName.lowercaseString + lastName.lowercaseString
SendBird.loginWithUserId(emailAddress, andUserName: nickname)
}
}
Finally, in the actual chat view controller, I attempt to start a messaging channel with another user:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
guard let targetId = session?.parent?.user?.email else {
return
}
// I logged in once with this id, so according to the SendBird docs, the
// user should exist.
SendBird.startMessagingWithUserId(targetId)
SendBird.setEventHandlerConnectBlock({ (channel) in
NSLog("%#", channel)
}, errorBlock: { (errorCode) in
NSLog("%D", errorCode)
}, channelLeftBlock: { (channel) in
}, messageReceivedBlock: { (message) in
}, systemMessageReceivedBlock: { (systemMessage) in
}, broadcastMessageReceivedBlock: { (broadcastMessage) in
}, fileReceivedBlock: { (fileLink) in
}, messagingStartedBlock: { (messagingChannel) in
SendBird.joinChannel(messagingChannel.getUrl())
// SendBirdChannelInfo is a custom struct for some use within the app
let channelInfo = SendBirdChannelInfo(
channelId: messagingChannel.getId(),
url: messagingChannel.getUrl()
)
// self.session is a custom data model for use with our database.
self.session?.sendBirdChannelInfo = channelInfo
SendBird.queryMessageListInChannel(messagingChannel.getUrl()).prevWithMessageTs(
Int64.max, andLimit: 30, resultBlock: { (queryResult) in
var maxMessageTs = Int64.min
for model in queryResult {
if maxMessageTs <= (model as! SendBirdMessageModel).getMessageTimestamp() {
maxMessageTs = (model as! SendBirdMessageModel).getMessageTimestamp()
}
}
SendBird.connectWithMessageTs(maxMessageTs)
}, endBlock: { (error) in
if let fetchMessagesError = error {
NSLog(fetchMessagesError.localizedDescription)
}
})
}, messagingUpdatedBlock: { (messagingChannel) in
}, messagingEndedBlock: { (messagingChannel) in
}, allMessagingEndedBlock: {
}, messagingHiddenBlock: { (messagingChannel) in
}, allMessagingHiddenBlock: {
}, readReceivedBlock: { (readStatus) in
}, typeStartReceivedBlock: { (typeStatus) in
}, typeEndReceivedBlock: { (typeStatus) in
}, allDataReceivedBlock: { (unsignedInt, thirtyTwoBitInt) in
}, messageDeliveryBlock: { (sent, message, data, messageId) in
}, mutedMessagesReceivedBlock: { (message) in
}) { (fileLink) in
}
}
With the exception of the lines I commented, this code comes directly from the SendBird manual. However, when it runs, I receive error code 14000, and the message, "Fail to start messaging," is logged.
What is the actual cause of the error? Am I missing a step during user login or SDK initialization, or is there another step in creating a channel? Or is it something else entirely?
Please try our new SDK instead of the old SDK which will be deprecated soon!
https://docs.sendbird.com/ios
Thanks!

How do I run a Cloud Code on Heroku?

With the Parse's announcement of their retirement, I have migrated my Parse Server onto Heroku. With my still neophyte knowledge of Heroku, I do not know if they have a similar function to that of Cloud Code, but I do know that a few months ago Parse Introduced a Heroku + Parse feature that allows you to run Cloud Code on any node.js environment, particularly Heroku.
My dilemma is, I have already migrated my server from parse to Heroku prior to learning about this feature :/ , so I cannot run any parse cloud code form my terminal because there is no existing server there anymore. So the question is, how can I emulate this following Cloud Code in Heroku & How do I adjust my swift?
Cloud Code:
// Use Parse.Cloud.define to define as many cloud functions as you want.
// For example:
Parse.Cloud.define("isLoginRedundant", function(request, response) {
Parse.Cloud.useMasterKey();
var sessionQuery = new Parse.Query(Parse.Session);
sessionQuery.equalTo("user", request.user);
sessionQuery.find().then(function(sessions) {
response.success( { isRedundant: sessions.length>1 } );
}, function(error) {
response.error(error);
});
});
and here is my swift back in xcode:
PFUser.logInWithUsernameInBackground(userName!, password: passWord!) {
(user, error) -> Void in
if (user != nil) {
// don't do the segue until we know it's unique login
// pass no params to the cloud in swift (not sure if [] is the way to say that)
PFCloud.callFunctionInBackground("isLoginRedundant", withParameters: [:]) {
(response: AnyObject?, error: NSError?) -> Void in
let dictionary = response as! [String:Bool]
var isRedundant : Bool
isRedundant = dictionary["isRedundant"]!
if (isRedundant) {
// I think you can adequately undo everything about the login by logging out
PFUser.logOutInBackgroundWithBlock() { (error: NSError?) -> Void in
// update the UI to say, login rejected because you're logged in elsewhere
// maybe do a segue here?
let redundantSession: String = "you are already logged in on another device"
self.failedMessage(redundantSession)
self.activityIND.stopAnimating()
self.loginSecond.userInteractionEnabled = true
}
} else {
// good login and non-redundant, do the segue
self.performSegueWithIdentifier("loginSuccess", sender: self)
}
}
} else {
// login failed for typical reasons, update the UI
dispatch_async(dispatch_get_main_queue()) {
self.activityIND.stopAnimating()
self.loginSecond.userInteractionEnabled = true
if let message = error?.userInfo["error"] as? String
where message == "invalid login parameters" {
let localizedMessage = NSLocalizedString(message, comment: "Something isn't right, check the username and password fields and try again")
print(localizedMessage)
self.failedMessage(localizedMessage)
}else if let secondMessage = error?.userInfo["error"] as? String
where secondMessage == "The Internet connection appears to be offline." {
self.failedMessage(secondMessage)
}
}
}
}
I would first checkout the example repo and read the parse-server documentation. Parse server supports cloud code out of the box and you simply specify which file contains your functions and triggers in the parse-server config. The link you posted with the integration between parse and heroku is not relevant for parse-server.

How to change PFUser password in Swift?

I've tried updating the same way you would update a PFUser's email and even tried converting obj-c code (from other questions); neither worked. I also have no idea how to use Cloud Code (well...I installed it but I don't know how to pass information into Cloud Code or how to use JavaScript). Is there a way to update a users password without having to send the reset email?
You can not change a user's password that way for security reasons. You have two choices
Password Reset Email
Cloud Code Function to Reset the Password
As I understand that you do not know JavaScript, here is a cloud code function that you can use to reset the user's password, as well as a way to call the function using Swift.
Function (in JavaScript):
Parse.Cloud.define("changeUserPassword", function(request, response) {
// Set up to modify user data
Parse.Cloud.useMasterKey();
var query = new Parse.Query(Parse.User);
query.equalTo("username", request.params.username); // find all the women
query.first({
success: function(myUser) {
// Successfully retrieved the object.
myUser.set("password", request.params.newPassword);
myUser.save(null, {
success: function(myUser) {
// The user was saved successfully.
response.success("Successfully updated user.");
},
error: function(myUser, error) {
// The save failed.
// error is a Parse.Error with an error code and description.
response.error("Could not save changes to user.");
}
});
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
});
Swift code to call the above function:
PFCloud.callFunctionInBackground("changeUserPassword", withParameters: ["username" : "MyCoolUsername", "newPassword" : passwordField.text]) {
(result: AnyObject?, error: NSError?) -> Void in
if (error == nil) {
// result is "Successfully updated user."
}
}
Good luck!
Yes, password can be changed without Cloud Code and e-mail. After changing "password" field for current user session is reset, but you can restore it by calling PFUser.logInWithUsername again.
let currentUser = PFUser.current()
currentUser!.password = "<new_password>"
currentUser!.saveInBackground() { (successed, error) in
if successed {
PFUser.logInWithUsername(inBackground: currentUser!.email!, password: currentUser!.password!) { (user, error) in
// Your code here...
}
}
}

Resources