Auto-complete text when answering a web service call

I'm new to ios programming, you need to implement something like google search box, i.e. autocomplete text box. My scenario is as follows 1. When the user enters a text field 2. The web service callback for the data ( data request = text field data ).

for example: - if the user type "abc" in the text field request data for calling the web service should be "abc", and the web service gives an answer to this. Now next time the user type is "d", where the text field contains "abcd", the response of the service should take into account the attached text. ( something like google search field ) 3.web service call should be asynchronous. 4.Sending should be displayed in the drop-down list.

Is this possible in ios ??? Any tutorial or example would be appreciated. Thanks in advance.

+7
source share
1 answer

I assume that you are talking about the services "Restful webservice" and "NOT SOAP" for the love of God!

Yes, of course it is . You can follow this approach, I could use HTTP-lib, for example AFNetworking , to make a request, but for simplicity I just init'ing NSData with the contents of the URL in the background and updating the user interface in the main stream using GCD .

  • Set the UITextField delegate to the ViewController that you are working on the viewDidLoad: method

     textField.delegate = self; 
  • override delegate method UITextField textField:shouldChangeCharactersInRange:replacementString: with

     - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ // To increase performance I advise you to only make the http request on a string bigger than 3,4 chars, and only invoke it if( textField.text.length + string.length - range.length > 3) // lets say 3 chars mininum { // call an asynchronous HTTP request dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSURL * url = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"http:/example.com/search?q=%@", textField.text]]; NSData * results = [NSData dataWithContentsOfURL:url]; NSArray * parsedResults = [NSJSONSerialization JSONObjectWithData: results options: NSJSONReadingMutableContainers error: nil]; // TODO: with this NSData, you can parse your values - XML/JSON dispatch_sync(dispatch_get_main_queue(), ^{ // TODO: And update your UI on the main thread // let say you update an array with the results and reload your UITableView self.resultsArrayForTable = parsedResults; [tableView reloadData]; }); }); } return YES; // this is the default return, means "Yes, you can append that char that you are writing // you can limit the field size here by returning NO when a limit is reached } 

As you can see, there is a list of concepts that you need to use for:

  • JSON parsing (I could parse XML, but why ?! JSON is better!)
  • HTTP request (you can use AFNetworking instead of what I did above)
  • Asynchronous HTTP requests (do not block the main thread)
  • GCD (material dispatch_async )
  • Delegates (in this case for UITextField)

Performance update

  • checking if the size exceeds 3 characters, you can even make an HTTP request every 2/3 characters, say, only a request if length % 3 .

I suggest you read something about these

+4
source

All Articles