iosquicktype

Customizing QuickType Suggestions in iOS


Well, I suspect that this is a forlorn question, ultimately doomed to sadden and disappoint me, but I want to have the question on the record for the future.

I will add this to the existing questions: here and here

The scenario is that I am creating an administration app that is designed to allow folks to edit the values in a database on a server. I have a fairly zippy API that can be used to allow REST-style data exchange.

I have the ability to download a list of values that the user can apply as search terms when they log in, and I'd like to be able to help them to enter things quickly.

For example, town names. If they enter "B", then I'd like to be able to offer "Bayshore", "Babylon" and "Bohemia" as suggestions, and so on.

A completely legit application.

I am under the assumption that there currently does not exist a QuickType API.

Am I wrong?


Solution

  • There is no QuickType API but you can get similar functionality with the ACEAutocompleteBar library from GitHub. Just for the record, I have listed the steps to get this working with Swift:

    1) Import the files from the folder "ACEAutocompleteBar" to your project.

    2) Create a bridging header and write #import "ACEAutocompleteBar.h" at the top.

    3) Make your view containing the text field an ACEAutocompleteDataSource and ACEAutocompleteDelegate.

    4) Implement the minimumCharactersToTrigger function

    func minimumCharactersToTrigger(inputView: ACEAutocompleteInputView!) -> UInt {
        return 1
    }
    

    5) Implement the inputView function.

    func inputView(inputView: ACEAutocompleteInputView!, itemsFor query: String!, result resultBlock: (([AnyObject]!) -> Void)!) {
    
        inputView.hidden = false
        inputView.alpha = 0.75
    
        if resultBlock != nil{
          dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
    
            var data:NSMutableArray = []
    
            if(self.myTextField.isFirstResponder()){    
              //query your data source with 'query' and add any of the results to data to pass back                
            }
            dispatch_async(dispatch_get_main_queue()) {resultBlock(data as [AnyObject])}  
          }
       }
    }
    

    6) Implement the textField function.

    func textField(textField: UITextField!, didSelectObject object: AnyObject!, inInputView inputView: ACEAutocompleteInputView!) {
        textField.text = String(object)
    }
    

    7) Call setAutocompleteWithDataSource on the textField.

    self.myTextField.setAutocompleteWithDataSource(self, delegate: self, customize: {
    
            inputView in
    
            // customize the view (optional)
            inputView.font = UIFont.systemFontOfSize(20)
            inputView.textColor = UIColor.whiteColor()
            inputView.backgroundColor = UIColor.blueColor()
            inputView.hidden = false
        })
    

    Hope that helps anyone looking for this kind of functionality!