I was reading an article that talks about Core Services and decided to use one of them. I import Core Services and add the library on XCode, but it doesn't work.
Sample code:
import Foundation
import CoreServices.DictionaryServices
func define(_ word: String) -> String? {
let nsstring = word as NSString
let cfrange = CFRange(location: 0, length: nsstring.length)
guard let definition = DCSCopyTextDefinition(nil, nsstring, cfrange) else {
return nil
}
return String(definition.takeUnretainedValue())
}
define("apple") // "apple | ˈapəl | noun 1 the round fruit of a tree..."
It gives a "no such module" error when I import it. I also look at Apple's documentation but there is no explanation for how to implement and use it.
The Solution:
Thanks to people who answered below my question, I made a little research and found the solution. Apparently, I can use the iOS dictionary by calling the UIReferenceLibraryViewController, but the Apple documentation says that we should not use this making for a dictionary app. It is obvious that it is not sufficient to make a dictionary app because it uses its own ViewController and is not customizable.
Here is the sample working code:
let dic = UIReferenceLibraryViewController(term: textLabelOutlet.text as! String)
dic.modalPresentationStyle = .popover // add this
let popover = dic.popoverPresentationController
popover?.sourceView = view
popover?.sourceRect = CGRect(x: 32, y: 32, width: 64, height: 64)
present(dic, animated: true)
Source: UIReferenceLibraryViewController cannot be presented as popup (always covers full screen)
CoreServicesmay be available on all platforms butDictionaryServicesseems to be available on macOS only.https://developer.apple.com/documentation/coreservices/1446842-dcscopytextdefinition
SDK lists only:
macOS 10.5+