iosobjective-cuitextfield

How to call an API runtime when I start typing in UITextfield?


Here is the one example:

enter image description here

Image 1 - It contain a UITextfield say emailTextField. When I start typing then according to its keyword in background one API will call which will gives response according to your keyword.

API used for searching is like

url/collaborator/search

and parameters are,

1. token 
2. term

Image 2 - When I started typing like "ma", then in background that API call has to call, i.e API call be like

url/collaborator/search?token=kdkhu67tdndodAK803i939ndAJDEw & term=ma

so it will give response, and that response it has to be show like drop down which is shown in image

Image 3 - If type specific name, then according to its data it has to filter the emails and specific email has to come, and when I click on it it has to add in textfield. I want single search as well as multiple email search.

url/collaborator/search?token=kdkhu67tdndodAK803i939ndAJDEw & term=komal

Currently I want to implement single at a time, if more than one can able do or add then no issue.

So, how to call API at run time when I start typing in textfield and get data in array?


Solution

  • Write below code,

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        NSLog(@"Data is : %@",_ccTextField.text);
    
    
        [self collaboratorApiMethod:_ccTextField.text];
    
        return YES;
    }
    
    
    // This metod is used to add collaborator, it will call API according to enetered data, JSON will receive
    -(void)collaboratorApiMethod:(NSString*)valueFromTextField
    {
       NSString *url =[NSString stringWithFormat:@"%@/collaborator/search?token=%@&term=%@",[userDefaults objectForKey:@"token"],valueFromTextField];
       // so this will generate in url
    
       // call api (REST API call)
       // you will get JSON data, store in array/dictionaries
    }
    

    Assume that, you got an array which contains a list of emails. Now that email you have to show suggestions when a user starts typing in textField and according to its value data will filter and show suggestion.

    write below code,

    - (UITableViewCell *)autoSuggestionField:(UITextField *)field tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath forText:(NSString *)text {
    
      userSearchDataCell *cell=[tableView dequeueReusableCellWithIdentifier:@"userSearchDataCellId"];
    
        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"userSearchDataCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }
    
        NSArray *emails = emailArray;
    
       if (text.length > 0) {
            NSPredicate *filterPredictate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@",text];
            months = [emailArray filteredArrayUsingPredicate:filterPredictate];
       }
    
    
        cell.emailLabel.text = emails[indexPath.row];
    }
    
    
     - (NSInteger)autoSuggestionField:(UITextField *)field tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section forText:(NSString *)text {
    
        if (text.length == 0) {
            return emailArray.count;
        }
    
        NSPredicate *filterPredictate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@", text];
         NSInteger count = [emailArray filteredArrayUsingPredicate:filterPredictate].count;
        return count;
    
    }
    
    
    - (CGFloat)autoSuggestionField:(UITextField *)field tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath forText:(NSString *)text {
        return 65;
    }
    
    
    - (void)autoSuggestionField:(UITextField *)field tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath forText:(NSString *)text {
        // NSLog(@"Selected suggestion at index row - %ld", (long)indexPath.row);
    
        NSArray *emails = emailArray;
    
        if (text.length > 0) {
            NSPredicate *filterPredictate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@", text];
            months = [emailArray filteredArrayUsingPredicate:filterPredictate];
        }
    
       self.emailLabel.text =  emails[indexPath.row];
    
    }