Implement Autocorrect in iOS 8 Keyboard Extension

3.5k views Asked by At

I'm creating a custom iOS 8 keyboard as a pet project.

I'm trying to replicate the system keyboard as accurately as possible, but building it from the ground up.

I'm largely done with this. The final hurdle I'm encountering is with adding autocorrect to my keyboard. Is there a way I can have the autocorrect behave as it would on the regular system keyboard?

The UILexicon documentation is quite sparse.

EDIT:

Making some progress with this. UILexicon's requestSupplementaryLexiconWithCompletion: method appears to only be returning results from my device's Contacts and keyboard shortcuts. I then went on to see how to autocorrect an NSString and found the UITextChecker class, which has been available since iOS 3.2.

Using this approach I can achieve autocorrect suggestions on individual words, but I'm still investigating the ability to add context-aware autocorrect (e.g. correcting "arctic monkeys" to "Arctic Monkeys").

2

There are 2 answers

3
Daniel On

From the documentation, it seems that UILexicon is to help you to create your own autocorrect, UILexicon has a bunch of UILexiconEntry entries which contain String pairs, the entry contains a userInput String which I assume its supposed to be what the user entered, and documentText which I assume is what you should be replacing that input with. You use func requestSupplementaryLexiconWithCompletion(_ completionHandler: ((UILexicon!) -> Void)!) From UIInputViewController to get this UILexicon.

I am assuming that the UIInputViewController knows what has been written to the documentProxy since it is the one relaying those messages, and thats how it knows what the user has input and in return what to put in the UILexicon..

This is what I gathered from reading the documentation, I have not tested it, though it should not be very hard to test this to verify..

I hope it helps

Daniel

2
Jeet On

Check out this simple but very effective auto correct implementation http://norvig.com/spell-correct.html

For auto completion you can implement a trie.