I'm looking for a way to access iOS dictionary programmatically. What I need is to retrieve the list of the words of a particular language from inside a custom app.
Is it possible? Are there any API that provide this (or similar) functionality?
I've checked Apple documentation but didn't find anything useful...May someone point me to the right direction?
I don't think there is a public API to access the embedded Dictionary. The closest is using UITextChecker but that may not satisfy your needs
As an alternative, you could find and embed a Word Dictionary in the Language you want? It may require some extra work
There is an API for bringing up the built-in dictionary UI (not the original question but raised in some of the responses), including checking for whether a given word is defined:
if ([UIReferenceLibraryViewController dictionaryHasDefinitionForTerm:@"word"]) {
UIReferenceLibraryViewController* ref =
[[UIReferenceLibraryViewController alloc] initWithTerm:@"word"];
[currentViewController presentViewController:ref animated:YES completion:nil];
}
I don't think there is a public API to access the embedded Dictionary. The closest is using UITextChecker but that may not satisfy your needs
As an alternative, you could find and embed a Word Dictionary in the Language you want? It may require some extra work
There is no API, but if all you need is a list of words you can read it from /usr/share/dict/words
:
NSString *allTheWords = [NSString stringWithContentsOfFile: @"/usr/share/dict/words"
encoding: NSUTF8StringEncoding
error: nil];
for (NSString *line in [allTheWords componentsSeparatedByString:@"\n"]) {
NSLog(@"%@",line);
}
If you need an arbitrary list of words you can get it from several online resources, here is one. The README at /usr/share/dict/README says iOS word list uses ftp://ftp.ox.ac.uk/pub/wordlists
For instant search you'll need to load the words on a structure suitable for prefix search, like a trie. Here is a sample project where I did just that.
If you need a dictionary, get one online, then include a pointer to the word entry in the trie node. If loading takes too much time you can experiment loading on the background, or compressing, or serializing the trie.
I ran into a similar problem a long time ago and did what other people suggested: embedded an English dictionary inside my app. Since then I've turned my solution into a static library which is available at www.lexicontext.com (note that it costs a few bucks). Considering the number of developers who downloaded it, this seems to be a common problem...
If you can use the private API you can use the following code to define a word:
_UIDictionaryManager *manager = [%c(_UIDictionaryManager) assetManager];
id definitions = [manager _definitionValuesForTerm:[self queryString]];
for(_UIDefinitionValue *item in definitions) {
myModel = self;
NSMutableAttributedString *def = [item definition];
NSString *str = [def string];
HBLogDebug(@"%@ = %@", [item localizedDictionaryName], str);
}
There is a PUBLIC API for this as @mrzzmr already stated - here the code in SWIFT:
if UIReferenceLibraryViewController.dictionaryHasDefinition(forTerm: "word") {
var childVC = UIReferenceLibraryViewController(term: "word")
someParentVC.present(childVC, animated: true, completion: nil)
}
Even there is no API, it can be used automatically if user long-press the selected word.
© 2022 - 2024 — McMap. All rights reserved.
NSDictionary
. – Jetton