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

前端 未结 3 1763
旧时难觅i
旧时难觅i 2020-12-17 02:16

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

相关标签:
3条回答
  • 2020-12-17 03:01

    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

    0 讨论(0)
  • 2020-12-17 03:03

    FYI: UIReferenceLibraryViewController.dictionaryHasDefinitionForTerm can't be used to display the result on your app. It seems those dictionary provider only authorized Apple to use their results, not any third party developer. Your app will be rejected:(

    0 讨论(0)
  • 2020-12-17 03:12

    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
    }
    
    0 讨论(0)
提交回复
热议问题