Reputation: 61
I'm using Parse and I'm retrieving a class from the data browser titled: "usernames". I get all the objects in the class, and store them in an array. I then want to search the array for a username, so that the user may login. I will do the same for the password. Here's my code:
- (IBAction)login:(id)sender
{
if ([usernameLogin.stringValue isEqualTo:@""] || [passwordLogin.stringValue isEqualTo:@""]) {
NSBeginAlertSheet(@"Error", @"OK", nil, nil, self.window, self, @selector(sheetDidEnd:resultCode:contextInfo:), NULL, NULL, @"Please fill in all fields.");
}
/* retrieve user from parse db */
PFQuery *usernameQuery = [PFQuery queryWithClassName:@"usernames"];
[usernameQuery findObjectsInBackgroundWithBlock:^(NSArray *usernames, NSError *error) {
NSString *userString = [NSString stringWithFormat:@"%@", usernameLogin.stringValue];
NSLog(@"USERS:\n %@", usernames);
int i;
for (i = 0; i < [usernames count]; i++) {
NSString *userFind = [usernames objectAtIndex:i];
if ([userString isEqualToString:userFind]) {
NSLog(@"FOUND!!!");
}
}
/*
if ([usernames indexOfObject:usernameLogin.stringValue]) {
NSLog(@"User: '%@' was found successfully!", usernameLogin.stringValue);
} else {
NSLog(@"User: '%@' doesn't exist in database, or password was incorrect!", usernameLogin.stringValue);
}
*/
}];
gameCont = [[CSGameController alloc] initWithWindowNibName:@"CSGameController"];
[gameCont showWindow:self];
[gameCont.window makeKeyAndOrderFront:nil];
_window.isVisible = false;
}
Can anyone explain what I'm doing wrong? I'm searching the database to see if the entered user exists. I setup a test user, and it still says it doesn't exist. Thanks so much! Added:
int i;
for (i = 0; i < [usernames count]; i++) {
NSString *userFind = [usernames objectAtIndex:i];
NSLog(@"UserFind class = %@, value = %@", [userFind class], userFind);
}
Output:
[2438:303] UserFind class = PFObject, value = <usernames:kx7aG2xfkX:(null)> {
username = ryan;
}
Upvotes: 0
Views: 135
Reputation: 61
Problem solved: I used Parse's User class. They have pre-existing methods for registering users, like I was trying to do here in my own classes. Their methods for registering are:
PFUser *user = [PFUser user];
user.email = emailField.stringValue;
user.username = usernameField.stringValue;
user.password = passwordField.stringValue;
[user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
Upvotes: 0
Reputation: 3725
It seems you're trying to do a lot of things here that just don't make sense. Please consider reading the Parse iOS Guide. All PFQueries return PFObjects. PFObjects are in many ways like NSDictionaries; they are a single record in a database and can be fetched or stored. A PFQuery always returns one or more PFObjects. A PFQuery's results should almost always be scopable to be immediately useful. In this example, the equals condition could have been articulated with -[PFQuery whereKey:equalTo:]. It is much faster to let Parse do the search for you. This also lets you create UI powered by the query's results via PFQueryTableViewController. Finally, please please please don't make your own login code. Your current code is easily hacked to not only allow anyone to log in as anyone, but to learn anyone's password as well. Use the built-in PFUser class for user accounts. It handles secure login, offline caching of credentials, password resets, email verification, you can let users log in with their Facebook or twitter accounts in addition to username/password, and Parse has built-in view controllers for logging in and creating accounts of PFUsers. PFUsers are also the way to secure your data; a PFACL is an Access Control List that lets you decide which PFUsers can read or write data.
Upvotes: 3
Reputation: 359
The wrong thing here that you are trying to use isEqualToString
: with userFind, which is PFObject
.
Try comparing with its username
property:
...
for (PFObject *aUsername in usernames)
{
NSString *userFind = [aUsername objectForKey:@"username"];
if ([userString isEqualToString:userFind]) {
NSLog(@"FOUND!!!");
}
}
Upvotes: 1