UILexicon in Objective-C
custom-keyboard, ios, ios8, objective-c, uilexicon
Solution
I've never used this feature, but a quick web search for "UILexicon" landed me in Apple's documentation; reading and following links from there filled in the picture pretty quick.
App Extension Programming Guide has a quick explanation of what lexicons are for:
Every custom keyboard (independent of the value of its `RequestsOpenAccess` key) has access to a basic autocorrection lexicon through the `UILexicon` class. Make use of this class, along with a lexicon of your own design, to provide suggestions and autocorrections as users are entering text.
Clicking the `UILexicon` link on that page took me to the reference doc for that class, which explains that it's a read-only list of Apple-provided term pairs. Each of its entries is a `UILexiconEntry` object -- the docs for that class say it provides a `userInput` (what the user typed, e.g. "ipad") and a `documentText` (what to substitute for it, e.g. "iPad"). Since those classes are read-only, it follows that they're probably not a way for you to provide your own autocorrection pairs -- as stated in the docs, they're for supplementing whatever autocorrection system you implement.
At this point, I don't even have to look at the doc for `requestSupplementaryLexiconWithCompletion:` to get a good idea how to use it: just the declaration tells me:
- It's a method on `UIInputViewController`, the class I'd have to subclass to create a custom keyboard. Somewhere in that subclass I should probably call it on `self`.
- Its return type is `void`, so I can't get a lexicon by assigning the result of a `requestSupplementaryLexiconWithCompletion` call to to a variable.
- It calls the block I provide, passing me a `UILexicon` object as a parameter to that block.
- It's got words like "request" and "completionHander" in it, so it'll probably do something asynchronous that takes awhile, and call that block when it's done.
So, I'm guessing that if I were writing a custom keyboard, I'd call this method early on (in `viewDidLoad`, perhaps) and stash the `UILexicon` it provides so I can refer to it later when the user is typing. Something like this:
@property UILexicon *lexicon;
- (void)viewDidLoad {
[super viewDidLoad];
[self requestSupplementaryLexiconWithCompletion:^(UILexicon *lexicon){
self.lexicon = lexicon;
}];
}
Because it's unclear how long `requestSupplementaryLexiconWithCompletion` will take to complete, any place where I'm using `self.lexicon` I should check to see if it's `nil`.
Back in the App Extension Programming Guide, it lists "Autocorrection and suggestion" under "Keyboard Features That iOS Users Expect", right before saying:
You can decide whether or not to implement such features; there is no dedicated API for any of the features just listed
So it sounds like autocorrection is something you have to do yourself, with your own UI that's part of the view presented by your `UIInputViewController` subclass. The API Quick Start for Custom Keyboards section in the programming guide seems to hint at how you'd do that: use `documentContextBeforeInput` to see what the user has recently typed, `deleteBackward` to get rid of it, and `insertText:` to insert a correction.
Problem
How do you use UILexicon in Objective-C? I find the documentation Apple provides is extremely unhelpful. What does it do? Does it return a dictionary or proper spellings of words? Or do I provide a word like "hellllo" and it matches it with the proper spelling "Hello" and returns that as a string? Any help would be appreciated. `requestSupplementaryLexiconWithCompletion:` Here's my error report, but obviously I'll have errors because I'm completely guessing how to use the function, no clue what goes inside the block statement (because the docs (at the time) don't say! (Beta 4 docs)) Hahahah!