smithyy
smithyy

Reputation: 220

Search usernames to get users with Parse.com iOS

i have implemented a search bar in iOS and i have to type in the exact username for a user to return. How can i change that so it returns users similar to what i searched. I am using parse.com as a backend.

    NSString *searchText = [self.searchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];


    //Check to make sure the field isnt empty and Query parse for username in the text field
if (![searchText isEqualToString:@""]) {

        PFQuery *query = [PFUser query];
        [query whereKey:@"username" equalTo:searchText];
        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            if (!error) {

                //check to make sure the query actually found a user
                if (objects.count > 0) {

                    //set your foundUser property to the user that was found by the query (we use last object since its an array)
                    self.foundUser = objects.lastObject;

                    //The query was succesful but returned no results. A user was not found, display error message
                } else {

                }

                //reload the tableView after the user searches
                [self.tableView reloadData];

            } else {

            }

        }];

in cellforRow:

PFUser *user = self.foundUser;
    if (self.foundUser) {

        //set the label as the foundUsers username
        cell.textLabel.text = self.foundUser.username;

how can i make it better so it shows similar users or users with the letters i type in?

Upvotes: 0

Views: 255

Answers (1)

Will
Will

Reputation: 1696

Have you tried Parse's PFQuery documentation? Looks like PFQuery has a whereKey:ContainsString: method that might fulfill your need.

https://parse.com/docs/ios/api/Classes/PFQuery.html#//api/name/whereKey:containsString:

Upvotes: 1

Related Questions