Specific User in Parse - ios

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"]

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
}

Query PFInstallation and add to channels key from cloud code

I am trying to get subscriptions added to all PFInstallations for a particular PFUser (for cases of one person using same login on iPhone iPad, etc.). I have a table view of all their Facebook friends who use the app as well. On selecting the friend in row, I want it to get my objectId and query all PFInstallations to get an array of all the PFInstallations where the key usersObjectId matches the PFUser objectId. Then, I can add a value to the channels of each of those PFInstallations. I have:
FriendArray *job = self.jobsArray[indexPath.row];
PFQuery *query = [PFUser query];
//job.facebookid is the Facebook id for that particular user
//each PFUser has a fbId and for those who logged in with Facebook, their Facebook ID is stored here
[query whereKey:#"fbId" equalTo:job.facebookid];
PFUser *user = (PFUser *)[query getFirstObject];
//This gives me the PFUser whose fbId value matches the Facebook id for the row that was selected
NSString *subscription = [#"User_" stringByAppendingString:user.objectId];
PFUser *me = [PFUser currentUser];
PFQuery *pushQuery = [PFInstallation query];
//Trying to get all PFInstallations that match the current user's usersObjectId, so I can add the value to each channel
[pushQuery whereKey:#"usersObjectId" equalTo:[me objectId]];
PFInstallation *allInstallations = (PFInstallation *)[pushQuery findObjects];
[allInstallations addUniqueObject:subscription forKey:#"channels"];
It tells me, though, that PFInstallation cannot be directly queried. How can I do the same thing in cloud code?
Ok, after hours of work, I finally figured it out, and thought would post the solution. If you want to edit all your PFInstallation entries of a certain type (I do this by always putting my PFUser objectId as a new value on PFInstallation), here you go.
For cloud code use:
Parse.Cloud.define("subscribingAll", function(request, response) {
Parse.Cloud.useMasterKey();
var usersObjectId = request.params.usersObjectId;
  var newSubscription = request.params.newSubscription;
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.equalTo("usersObjectId", usersObjectId);
pushQuery.find({
success: function(results) {
response.success(results);
},
error: function() {
response.error("failed");
}
});
});
Then, on your app, you need a PFCloud call to handle all this.
[PFCloud callFunctionInBackground:#"subscribingAll"
withParameters:#{#"usersObjectId": [me objectId], #"newSubscription": subscription}
block:^(NSArray *theCount, NSError *error) {
if (!error) {
int i;
for (i = 0; i < [theCount count]; i++) {
PFInstallation *change = [theCount objectAtIndex:i];
[change addUniqueObject:subscription forKey:#"channels"];
[change saveInBackground];
}
}
}];
The cloud code returns an array where each object is the data from PFInstallation matching the query. You need to run this through a loop, and set each objectAtIndex as a PFInstallation. From there, just do a simple addUniqueObject, and voila, you are done.
In my case, when logging in, it duplicates the objectId to a key called usersObjectId that I made for PFInstallation. So, I login on my iPhone and then again on my iPad, I have 2 different PFInstallations but both with the same usersObjectId. Running all this code allows me to isolate all of my owned Installations and edit them, specifically to go along with the code I use for subscribing to Facebook friends, so that I can be notified when they post something.
It looks like you can only modify a PFInstallation on the current device, and not all from one user or in the cloud.
This is the code shown on parse :
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation addUniqueObject:#"Giants" forKey:#"channels"];
[currentInstallation saveInBackground];
Maybe a workaround could be adding a new default channel that you subscribe every user to that sends them a notification if they should subscribe to a new channel? I'm unsure if this is the best solution, as I do not know in what context you are performing this subscription.

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.

Scheduling Background jobs in Parse

i have searched a lot but can find any thing about parse background jobs scheduling .i am new in parse please give me example how and where to implement background jobs with scheduler in parse as there is no example any where
You schedule background jobs in the dashboard. Not possible in code
To get detailed knowledge about parse you must go through its guide which is available at https://www.parse.com/docs/ios_guide#top/iOS
As per your question, most of the work occurs in background for parse, for e.g.
-if you want to update user detail you can use without completion block:
PFUser *user = [PFUser currentUser];
user [#"firstname"] = self.firstnameTextFeild.text;
[user saveInBackground];
-if you want to update user detail you can use with completion block:
PFUser *user = [PFUser currentUser];
user [#"firstname"] = self.firstnameTextFeild.text;
[user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
//Do anything you want
}];
-if you want to update user detail even if you are offline:
PFUser *user = [PFUser currentUser];
[#"firstname"] = self.firstnameTextFeild.text;
[user saveEventually]; // This will update user as device gets online

QBUser from PFUser

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.

Resources