iOS custom keyboard base? - ios

I need to create a custom keyboard that looks/feels pretty much the same like the system keyboards but is for a language that iOS doesn't have:
whenever I have to type using the system keyboard, I'm subject to the autocorrect, which not only gives wrong options, but also learns wrong words for that keyboard's language.
the language I need doesn't use 3 of the 26 Latin letters but it does need diacritics in some others as well as the ' quite often, so it would be nice to repurpose 3 of the keys for that.
My problem is that I'm not interested in creating a keyboard from bare UIViews just to do what in my opinion amount to tweaks to the existing system keyboards. I was dumbstruck when I found out that apparently I do have to recreate the whole experience myself instead of having some Apple-provided basis to build upon. I also can't see most developers being thrilled, so I began to think I may be wrong and there is something we can use after all. Can anyone enlighten me?

Related

iOS detect keyboard layout (e.g. QWERTY, AZERTY)

I am building a custom suggestion/autocorrection feature in an iOS app. It must detect accidental adjacent keypresses and compare this to a known word list to suggest the word it thinks the user intended to type.
For example, if the custom word list contains cat, dog, monkey, and the user types cst, the app can determine that the most likely word was cat (because s is adjacent to the a key)
This will work on a standard QWERTY keyboard, but what happens if the user is using an AZERTY keyboard?
For the autocorrect/suggest to work reliably, the app must be able to detect the keyboard layout in use.
In iOS, it is possible to obtain a UITextInputMode object from a UITextField. This object has a primaryLanguage (string) property, which will display the locale (e.g. en-GB), but this does not contain enough granularity to distinguish between English (Australia) QWERTY and English (Australia) AZERTY. In both cases, the primaryLanguage is en-AU.
Is it possible to detect the keyboard layout in iOS?
I have not been able to find a clean solution to this problem.
Maybe this would be worth a TSI ticket to discuss it with Apple employees.
I know that this will not be a satisfying answer, but I would still like to share my thoughts here for future readers:
Private API of UITextInputMode:
textField.textInputMode?.value(forKey: "identifierWithLayouts")
This will return a string like de_DE#sw=QWERTZ-German;hw=Automatic from which you can infer the keyboard layout.
UserDefaults
UserDefaults.standard.object(forKey: "AppleKeyboards")
This will return a list of all keyboards that the user has installed. In most cases, this will only be one language (besides the emoji keyboard).
For example:
Optional(<__NSCFArray 0x600003b8e6c0>(en_US#sw=QWERTY;hw=Automatic,emoji#sw=Emoji))
You could also iterate over UserDefaults.standard.dictionaryRepresentation() and search for QWERTZ/QWERTY/AZERTY within the values.
With much manual effort, you could maybe encode UITextInputModes to binary data in all ambiguous cases like en_AU. Something like
NSKeyedArchiver.archivedData(withRootObject:textField.textInputMode, requiringSecureCoding: false) can then be used to compare binary encodings of the user's textInputMode at runtime.
I have found this old question that may have a solution for you. No sure it's still working, but it shows in the question how to get the current instaled keyboards, and someone provided a "gray area" solution, as it seems that there is no direct way to achieve what you intend to do.
Hope this help.
AppleLanguages object at index 0 is common way to get input language. Instead of trying to determinate which language users are using and default layout, as far I worked with custom keyboard extension, I used one of recommended ways from Apple: use separate keyboard layout for each language. In other way I don't think you will have a stable and productive prediction, auto correct, by the way. As for auto correct I used SymSpell (https://github.com/AmitBhavsarIphone/SymSpell) and different dictionaries from https://github.com/wolfgarbe/SymSpell/tree/master/SymSpell.FrequencyDictionary to make my own RealmDb for each language. So far it was a little work to do, but finally my keyboard extension was publisehd in App Store. [Note: I am not related with SynSpell owners or coders] See images

Autocorrection and Suggestion for custom keyboard

I want to add Autocorrection and Suggestion to my custom keyboard.
There are already several similar questions on Stack, but there is only said about UILexicon which as I understood is only used to get user's shortcuts and that there is no way to acces Apple Autocorrection and Suggestion library.
I also saw some questions regarding UITextChecker but don't know if it has an access to Apple's native sugesstion library?
May be there are some new classes for that?
I use four different systems for my keyboard:
I have a list of the top 30,000 or so words, ranked in order of the most used to the least used. You can pay for lists, I just got a free one of about 42,000 and edited it down a lot.
guessesForWordRange is provided by Apple. It will guess words that are close to what you have typed. It does a fairly good job, but I had to filter out some things. The top guess sometimes has quotation marks around it, but other then that it works great.
completionsForPartialWordRange is also provided by Apple. It will return completed words, but in alphabetical order, not ranked by usage. Not much good on it's own, but is a great supplement to 1 and 2. (if this worked correctly #1 wouldn't be needed)
Special cases. Mainly for contractions. When someone types didnt, I wanted it to auto choose didn't. So I have almost all contractions specifically programmed in.
So my word suggestions and autocorrection aren't perfect, but it does a decent job.
Hope this helps.
Edit: As of iOS 16 it seems completionsForPartialWordRange is working correctly, so having your own list of words shouldn't be needed anymore.

Custom text field autocorrection

Is it possible to customize the autocorrection of the keyboard when entering text into a text field?
In my app, I have a text field where rather long technical terms have to be entered. If I could provide an additional dictionary with frequent terms, it would incredibly help my users.
I've searched the documentation of the current iOS version and had a peek at the new iOS 8 documentation. QuickType seems similar to what I'm looking for. But is it configurable?
I hope it's doable without creating a custom keyboard. I don't think that's the way to go.
No, at the moment of writing it is simply not possible to add or alter the autocompletion or autocorrect library that Apple uses in combination with it's keyboards. Apple just doesn't allow "tampering" with it's autocompletion/correction and probably for good reason since they are context aware and self learning.
Alternatives you can consider:
1. Create your own keyboard with your own "quick type"
Probably not the best option, since you don't want to alter the keyboard but only autocompletion.
2. Add some kind of autocompletion in your app
There are some autocomplete libraries you could look into like HTAutoCompleteTextField and there are probably other CocoaPods you could look into. I think this would be your best option.
You could also write some sort of autocomplete yourself that shows the options while you type in a table or other kind of UI if you can't find a library that suits you. Not trivial, but could be worth it for your users.

How can I access localisable strings for standard iOS system terms (E.g. Favorites, More...)?

I don't know if my approach to this is fundamentally wrong, but I'm struggling to get my head around a (seemingly trivial?!) localisation issue.
I want to display the title of a 'System' UITabBarItem (More, Favorites, Featured, etc...) in a navigation bar. But where do I get the string from? The strings file of the MainWindow.nib doesn't contain the string (I didn't expect it to) and reading the title of the TabBarItem returns nil, which is what stumped me.
I've been told, there's no way to achieve it and I'll just have to add my own localised string for the terms in question. But I simply don't (want to) believe that!! That's maybe easy enough in some languages, but looking up, say, "More" in already presents me with more than one possible word in some languages. I'm not happy about simply sending these words for translation either, because it still depends on the translator knowing exactly which term Apple uses. So am I missing something simple here? What do other people do?
Obviously, setting the system language on my test device and simply looking to see what titles the Tab Items have is another 'obvious' possibility. But I really have a problem with half baked workarounds like that. That'll work for most languages, but I'm really gonna have fun when it comes to Russian or Japanese.
I'm convinced there must be a more reliable way to do this. Surely there must be a .strings file somewhere in the SDK that has these strings defined?
Thanks in advance...
Rich
The simple and unfortunate answer is that aside from a very few standard elements (e.g. a Back button), you need to localize all strings yourself. Yes, UIKit has its own Localization.strings file but obviously that's outside of your app sandbox so you don't have access to it.
I filed a bug with Apple years ago about providing OS-level localization for common button titles, tab item labels, etc. That bug is still open but obviously they haven't done it yet (sorry, I don't have the radar # handy).

Is it easy to make 2 language versions for iPhone (Japanese)?

I have a client who I am pricing an app for, however other than the English version they would also like a Japanese version. Has anyone had experience in a similar case, is there an easy way to do it? Do I need to create two versions, one English and one Japanese? If it were two Latin languages I could imagine it would be easier but Japanese write from top to bottom, right to left so this worries me.
You don't seem to know a lot about Japanese. They're perfectly accustomed to western-style left-right, top-down writing, especially due to the influence of computers. You can of course create separate views (views only, no need for separate apps) for Japanese that switch everything to top-down, right-left writing. But it's only a minority of apps that do that. In fact, the Daijirin Japanese-Japanese dictionary is the only example I know of.
Talk to your client what kind of Japanese localization he wants. Odds are, he just wants strings replaced. See #kelloti's answer.
As a general advise: Make sure you get a native translator/developer who can guide you in a good localization. Don't simply copy-paste in strings you get from somebody else that you have no idea how to even read. This only produces terribly localized versions.
Read the Apple documentation on internationalization. I don't think you should have many issues with Japanese (how else would they sell phones in Japan?)

Resources