Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - open built-in iOS Dictionary to find word meaning

In my project I'd like to open iOS built-in Dictionary to find word meaning, or even better, get the meaning of the word directly in my app.

At the moment I found how to check a string is spelled correctly using UITextChecker

func wordIsReal(word: String) -> Bool {
    let checker = UITextChecker()
    let range = NSMakeRange(0, count(word))
    let misspelledRange = checker.rangeOfMisspelledWordInString(word, range: range, startingAt: 0, wrap: false, language: "en")

    return misspelledRange.location == NSNotFound
}
like image 627
RiccardoCh Avatar asked Sep 16 '15 08:09

RiccardoCh


People also ask

How do you use a dictionary to find a word?

Step-by-step guide to using a dictionarySTEP 1 - Find the word you want to look up. STEP 2 - Find the letter that the word begins with. STEP 3 - Open the dictionary to the page with the relevant letter, in this case the letter C. STEP 4 - Now look at the second letter in the word you are looking for.

Does iOS have a built in dictionary?

Apple's built-in dictionary feature works for devices running on iOS 11 and later. To enable the feature: Head to Settings > General > Dictionary. Tap one or more of the dictionaries you wish to download.


2 Answers

I found the solution for Objective-C:

if ([UIReferenceLibraryViewController    dictionaryHasDefinitionForTerm:@"word"]) {
    UIReferenceLibraryViewController* ref = 
    [[UIReferenceLibraryViewController alloc] initWithTerm:@"word"];
    [currentViewController presentViewController:ref animated:YES completion:nil];
}

and I've edited it for Swift 3:

let word = "home"
if UIReferenceLibraryViewController.dictionaryHasDefinitionForTerm(word) {
        let ref: UIReferenceLibraryViewController = UIReferenceLibraryViewController(term: word)
        self.presentViewController(ref, animated: true, completion: nil)
}

and the same for Swift 4:

let word = "home"
if UIReferenceLibraryViewController.dictionaryHasDefinition(forTerm: word) {
    let ref: UIReferenceLibraryViewController = UIReferenceLibraryViewController(term: word)
    self.present(ref, animated: true, completion: nil)
}

This solution allows to open the built-in dictionary if the word has a definition in the dictionaries saved in the device

like image 195
RiccardoCh Avatar answered Oct 17 '22 10:10

RiccardoCh


Try with this

func wordIsReal(word: String) -> Bool {
    let checker = UITextChecker()
    let range = NSMakeRange(0, count(word))
    let misspelledRange = checker.rangeOfMisspelledWordInString(word, range: range, startingAt: 0, wrap: false, language: "en_US")
    NSLog("misspelledRange:\(misspelledRange)")
    NSLog("word:\(word)")
    var arrGuessed:NSArray? = checker.guessesForWordRange(misspelledRange, inString: word, language: "en_US")as NSArray!
  NSLog("arrGuessed:\(arrGuessed)")
    //var correctedStr = textAsNSString.stringByReplacingCharactersInRange(misspelledRange, withString: arrGuessed.objectAtIndex(0) as String)
    return misspelledRange.location == NSNotFound
}
like image 38
Jamil Avatar answered Oct 17 '22 12:10

Jamil