如何通过编程调用TextView中的「查找建议」(Siri搜索建议)功能?
Hey there! Great question—this is totally doable on iOS using Apple's native APIs. I've implemented something similar before, so let me walk you through how to pull it off:
前提说明
First off, this is an iOS-specific system feature, so all examples below use Swift and iOS SDK APIs.
Step 1: Capture the Selected Text
First, you need to grab the highlighted text from your UITextView—either what the user has selected, or a range you programmatically set:
guard let selectedRange = textView.selectedTextRange, let lookupText = textView.text(in: selectedRange) else { // No text selected, exit early return }
Step 2: Trigger the System "Look Up" Interface
There are two solid ways to launch the native look-up experience (including Siri search suggestions, dictionary definitions, and web results):
Option 1: Use UIReferenceLibraryViewController (Official, Public API)
This is Apple's recommended way to show the system look-up UI. It handles dictionary definitions first, and falls back to broader search if needed:
let lookupVC = UIReferenceLibraryViewController(term: lookupText) // Check if the term has a dictionary definition first if UIReferenceLibraryViewController.dictionaryHasDefinition(forTerm: lookupText) { present(lookupVC, animated: true) } else { // If no dictionary entry exists, trigger system-wide search via URL scheme guard let encodedText = lookupText.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed), let searchURL = URL(string: "x-apple-data-detectors://?type=look-up&query=\(encodedText)") else { return } if UIApplication.shared.canOpenURL(searchURL) { UIApplication.shared.open(searchURL, options: [:], completionHandler: nil) } }
Option 2: Add a Custom "Look Up" Menu Item
If you want to replicate the exact flow where users highlight text and tap a "Look Up" option (just like the native TextView), you can add a custom menu item to the text view's interaction menu:
override func viewDidLoad() { super.viewDidLoad() // Create and add the custom menu item let lookUpItem = UIMenuItem(title: "Look Up", action: #selector(handleLookUpTapped)) UIMenuController.shared.menuItems = [lookUpItem] // Ensure the text view can accept user interaction textView.isUserInteractionEnabled = true textView.becomeFirstResponder() } @objc private func handleLookUpTapped() { // Reuse the text capture and lookup logic from Step 1 & Option 1 guard let selectedRange = textView.selectedTextRange, let lookupText = textView.text(in: selectedRange) else { return } let lookupVC = UIReferenceLibraryViewController(term: lookupText) if UIReferenceLibraryViewController.dictionaryHasDefinition(forTerm: lookupText) { present(lookupVC, animated: true) } else { guard let encodedText = lookupText.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed), let searchURL = URL(string: "x-apple-data-detectors://?type=look-up&query=\(encodedText)") else { return } if UIApplication.shared.canOpenURL(searchURL) { UIApplication.shared.open(searchURL, options: [:], completionHandler: nil) } } }
Key Notes
- The
x-apple-data-detectorsURL scheme is a native iOS way to trigger system-wide look-up, which includes Siri's search suggestions for non-dictionary terms. - Make sure your app has network access enabled if you want to load online results (like web suggestions or Siri data)—most apps already have this enabled by default.
- For macOS with
NSTextView, the logic is similar but usesNSReferenceLibraryViewControllerinstead.
内容的提问来源于stack exchange,提问作者BillTheMagnificient

