Android与iOS设备词库访问咨询:单词验证及自动纠错词源获取
Hey there! Let me walk you through how to handle word validation and access to autocorrect dictionaries on Android and iOS, based on my hands-on experience with both platforms.
Validating if a String is a Valid Word
Android doesn't expose a direct public API to access the system's built-in dictionary, but you can leverage the SpellCheckerSession class—this is the official interface to the system's spell-checking service, which uses the built-in dictionary under the hood.
Here's a quick Kotlin example of how to use it to verify a word:
val textServicesManager = getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE) as TextServicesManager val spellCheckerSession = textServicesManager.newSpellCheckerSession( null, Locale.getDefault(), object : SpellCheckerSession.SpellCheckerSessionListener { override fun onGetSuggestions(results: Array<out SuggestionsInfo>?) { results?.firstOrNull()?.let { info -> // Check if the original word exists in suggestions (marking it as valid) val isWordValid = (0 until info.suggestionsCount) .map { info.getSuggestionAt(it) } .contains(yourTargetWord) // Use the isWordValid flag in your logic } } override fun onGetSentenceSuggestions(results: Array<out SentenceSuggestionsInfo>?) {} }, false ) spellCheckerSession.getSuggestions(TextInfo(yourTargetWord), 1)
Keep in mind this depends on the device having a default spell-checker enabled (most stock Android devices do, but custom ROMs might differ).
Accessing the Autocorrect Dictionary
Unfortunately, there’s no way to directly access the full system autocorrect dictionary on Android. The dictionary is tied to the active input method (like Gboard) and is either closed-source or stored in private, inaccessible locations. If you need custom autocorrect logic, your best bet is to integrate an open-source dictionary library (like Hunspell) or curate your own word list.
Validating if a String is a Valid Word
iOS makes this straightforward with the UITextChecker class, part of UIKit, which uses the system's built-in dictionary for spell checks.
Here's a Swift example to check word validity:
func isWordValid(_ word: String) -> Bool { let checker = UITextChecker() let range = NSRange(location: 0, length: word.utf16.count) let misspelledRange = checker.rangeOfMisspelledWord( in: word, range: range, startingAt: 0, wrap: false, language: Locale.current.languageCode ?? "en" ) // If no misspelled range is found, the word is valid return misspelledRange.location == NSNotFound }
Accessing the Autocorrect Dictionary
Just like Android, iOS doesn’t provide public APIs to access the full system autocorrect dictionary. The underlying dictionary is part of Apple’s private frameworks, and direct access would violate App Store guidelines. If you need custom autocorrect functionality, you can use UITextChecker's suggestions(forWordRange:in:language:) method to get correction suggestions for specific words, or integrate a third-party spell-check library (like Hunspell for iOS) or build your own curated word list.
- Both platforms' built-in dictionaries can vary by locale, input method, and system version—so results might not be 100% consistent across all devices.
- For more control over word validation and autocorrect, using a custom or open-source dictionary is often more reliable than relying solely on system APIs.
内容的提问来源于stack exchange,提问作者user5830749

