QBUser from PFUser - ios

I have an existing ios app that was created using the Parse PFLoginController for Facebook/Twitter login. I want to add a new tab for video chat and was wondering if there is a simple way to create a QBUser out of an existing PFUser to use for chat.
Thanks,
EE

PFUser class reference https://www.parse.com/docs/ios/api/Classes/PFUser.html
QBUser class reference http://sdk.quickblox.com/ios/Classes/QBUUser.html
PFUser *pfUser = [PFUser currentUser];
QBUUser *user = [QBUUser user];
user.ID = pfUser.objectId;
user.login = pfUser.username;
user.email = pfUser.email;
user.password = pfUser.password;
If you use Facebook login - it's also easy to do it - you just need Facebook access token.
You can get QBUser from Facebook access token
[QBUsers logInWithSocialProvider:#"facebook" accessToken:#"AAAGmLYiu1lcBADxROiXg4okE80FQO1dJHglsbNT3amxmABnmBmhN6ACbgDqNC3H4Y9GmZAdoSfPUkI9O7ZBJvKQCewNZAp3SoxKCNIMwQZDZD3" accessTokenSecret:nil delegate:self];
- (void)completedWithResult:(Result *)result{
if(result.success && [result isKindOfClass:QBUUserLogInResult.class]){
QBUUserLogInResult *res = (QBUUserLogInResult *)result;
QBUUser *user = res.user;
// Login to chat to use VideoChat features
QBUUser *currentUser = [QBUUser user];
currentUser.ID = user.ID;
currentUser.password = [QBBaseModule sharedModule].token; //Facebook/Twitter authentication: use session token as password
// set Chat delegate
[QBChat instance].delegate = self;
// login to Chat
[[QBChat instance] loginWithUser:currentUser];
}
}

I've never heard of QB until now. It looks like a cool service, so I'll have to check it out. That said, since I've never messed with it, this is all theoretical.
I've got a couple apps that I've built a generic chat implementation using Parse, so it would be neat to transition to a true messaging based platform. The way I would do it, instead of having people sign up for two different accounts, if you still need the Parse back-end for data persistence, is to have your Parse users just be anonymous. When creating PFObject data for Parse, just create a field for the QBUser's ID number. Then if you need to find info pertaining to that anonymous user, set your PFQuery to search for the ID field for the given QBUser.
It's not completely thought through but I could see it working out.

Related

PFUser currentUser returns nil

I'm using Parse SDK, login and signup are working perfectly : [PFUser currentUser] is returning the current user.
But after restarting app, [PFUser currentUser] is returning nil.
Why is the application not persisting the session ?
Login code (from parse.com) I'm using :
[PFUser logInWithUsernameInBackground:self.username.text password:self.password.text
block:^(PFUser *user, NSError *error) {
if (user) {
// Do stuff after successful login.
} else {
// The login failed. Check error to see why.
}
}];
EDIT 2: Create new project, it works. I don't know how and why, but it works.
EDIT: There is no logoutin the whole project
It looks like you are calling .logout somewhere, in which case the app will return nil for [PFUser currentUser].
The current user will persist from session to session if you do not automatically log out the user.
PFUser *currentUser = [PFUser currentUser];
if (currentUser) {
//save the installation
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
currentInstallation[#"installationUser"] = [[PFUser currentUser]objectId];
// here we add a column to the installation table and store the current user’s ID
// this way we can target specific users later
// while we’re at it, this is a good place to reset our app’s badge count
// you have to do this locally as well as on the parse server by updating
// the PFInstallation object
}

Pulling User Information from Parse Database

I am using Parse as my backend for my app. Once the user logs into their account I am trying to get the next view to say "Welcome, (First Name)" at the top. However, I cannot seem to figure out how to accomplish this even using Parse's online documents. Their site directed me here for further assistance. I have tried using their query feature, but could not figure it out. In other words, I am trying to pull the current logged in user's first name, from the database and display it once logged in.
Current code:
PFQuery *query = [PFUser query];
[query whereKey:#"firstName" equalTo:currentUser]; // find user's first name
NSArray *firstName = [query findObjects];
Previous code:
PFUser *currentUser = [PFUser currentUser];
if (currentUser) {
// do stuff with the user
Welcome.text = [NSString stringWithFormat:#"Welcome,", currentUser];
Your overcomplicating things.
You don't have to execute a query every time the view loads, instead you should put this in a plist or NSUserDefaults as not to use an API request simply to display the current users name.
However, you can do the following to the current users username :
if ([PFUser currentUser]) {
Welcome.text = [NSString stringWithFormat:#"Welcome, %#", [PFUser currentUser].username];
}
First of all you should check if you actually sign-up and/or logged into Parse with this kind of function:
[PFUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
}];
[PFUser logInWithUsernameInBackground:#"My username" password:#"My password" block:^(PFUser *validUser, NSError *error) {
}];
Check this link: https://parse.com/docs/ios_guide#users-signup/iOS
After you did this, whenever you you want to retrieve your user information,
[PFUser currentUser] is the right way to call some information:
Say for example you want to retrieve the objectId you can get it like this:
NSString *str = [PFUser currentUser].objectId;
Or say you want to set a custom value like this:
NSString *str = #"My custom object";
[PFUser setObject:str forKey:#"MyCustomObject"];
[PFUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
}];
Than you retrieve it like this:
NSString *str = [PFUser objectForKey:#"MyCustomObject"];
You should really check the documentation and example by Parse that are really well explained ! ;)
(Here are some tutorials/Examples by Parse: https://parse.com/tutorials)
How is the transition from your login to your main view set up? Are they both two different controllers?
If so, you should look into NSNotificationCenter...
In your MainViewController, implement
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(loadObjects)
name:#"loginFinished"
object:nil];
And loadObjects will be
- (void)loadObjects
{
Welcome.text = [NSString stringWithFormat:#"Welcome %#", [[PFUser currentUser] objectForKey:"userNameField"]];
}
* You need to parse the PFUser object to access its fields. Its just a dictionary so supply it a key 'username' or whatever, and you receive a value 'myusername'. *
Then in your LoginViewController, within your [PFUser logInWithUsernameInBackground:password:block
Implement this
[PFUser logInWithUsernameInBackground:#"My username" password:#"My password" block:^(PFUser *validUser, NSError *error) {
if (!error) {
[[NSNotificationCenter defaultCenter] postNotificationName:#"loginFinished" object:nil];
}
}];
But this is basically how you should setup your login->main flow. Learn NSNotifications, Delegation, and maybe KVO... Learning these will make you a understand how data can be passed around in the iOS/Mac environment.
Heres an analogy for all of them:
NSNotification: A teacher(NSNotification poster) announces a test to all his students(NSNotification observer), or at least the one's who are currently in class, students missing class aren't observing.
Delegate: A student finishes a test and informs the professor(delegate).
KVO: A student(KVO poster) completes a question and raises their hand where the teacher or even other students could be KVOs (key-value observers) and act on their action.

Parse.com can't get correct username from Parse

I am trying to implement Facebook auth with Parse SDK as described in officialy manual.
But instead of real name I am getting some token when calling [PFUser currentUser]
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
if ([PFUser currentUser] && [PFFacebookUtils isLinkedWithUser:[PFUser currentUser]]) {
NSLog(#"USER == %# == IS ALREADY LINKED WITH FACEBOOK", [PFUser currentUser]);
}
}
NSLOG shows me this: USER == Op2Qz1RxiwR1zflsSzCVR538A == IS ALREADY LINKED WITH FACEBOOK
What could be the issue?
In your code you are telling it to log the entire user object. This will probably log something like a unique I'd or something.
If you want the name then you should use...
[PFUser currentUser].userName
Or some other property.
It doesnt know which part of the user to log unless you actually tell it which part you want to log.

Specific User in Parse

I am using a parse database to store data in my iOS application, i can not provide any code. Is there a way to have an app that uses parse to hold login information in a user class but also use that information as a sorted storing method. For example(User A can only get what User A saved). I already have a login completed i just need to finish this part.
Any help is appreciated.
Thanks in advance!
You have to store what user A has saved by using
PFUser *user = [PFUser currentUser];
user[#"gender"] = #"male";
[user saveInBackground];
when you want to access it use this
PFUser *user = [PFUser currentUser];
NSString*gender = [user valueForKey:#"gender"]

Using a Hidden Twitter Account

Not sure if any one has tried this. I want to access twitter with an account that the user of the phone doesn't have access to.
The way I have looked at it is by adding a twitter account using the OAth (token/secret)
I can add an account using the following code.
NSString *token = #"blahblahblah";
NSString *secret = #"blahblahblah";
ACAccountStore *store = [[ACAccountStore alloc] init];
ACAccountCredential *credential = [[ACAccountCredential alloc] initWithOAuthToken:token tokenSecret:secret];
ACAccountType *twitterAcctType = [store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
ACAccount *newAccount = [[ACAccount alloc] initWithAccountType:twitterAcctType];
newAccount.credential = credential;
[store saveAccount:<#(ACAccount *)#> withCompletionHandler:<#^(BOOL success, NSError *error)completionHandler#>
[store saveAccount:newAccount withCompletionHandler:^(BOOL success, NSError *error) {
if (success) {
NSLog(#"Account was saved!");
} else {
//something went wrong, check value of error
}
}];
But this code stores the user id associataed with the secret in the iphones account store. The problem being that if the phone user then uses twitter the account privilages are still there for him to use...
There doesn't seem to be a store deleteAccount method implemented
For my second attempt i tried using just the user that was created without adding it to the account store but I think the SaveAccount method is what verifies the token and secret and gets the user name...
Any tips???
Just a friendly reminder that this will most likely have your app banned from the app store, as this can easily be classified as spying on the user/device, if you can pull it off which I doubt.
Also, beware of having thousands/millions of users twitting from the same account.
Finally, there does not seem to be a way to remove the account, as you said, plus in order to use it you'd need to use requestAccessToAccountsWithType:withCompletionHandler: which would trigger a notice to the user and would make them think you were requesting access to his tweeter account, rather than yours...

Resources