I'm building a macOS app that uses WKWebView for text editing (not NSTextView). I need to provide grammar checking by calling NSSpellChecker programmatically and sending results back to the web editor.
The problem: TextEdit (which uses NSTextView) catches grammar errors like "Can I has pie?" and "These are have" — but when I call NSSpellChecker's APIs directly, those same errors are never flagged.
I've tried both APIs:
1. The unified check() API:
let results = checker.check(
text, range: range,
types: NSTextCheckingAllTypes,
options: [:],
inSpellDocumentWithTag: tag,
orthography: &orthography,
wordCount: &wordCount)
This returns only .orthography results (language detection). No .spelling, no .grammar — just orthography.
2. The dedicated checkGrammar(of:startingAt:...) API:
let sentenceRange = checker.checkGrammar(
of: text,
startingAt: offset,
language: nil,
wrap: false,
inSpellDocumentWithTag: tag,
details: &details)
This catches sentence fragments ("The.", "No.") and some agreement errors ("The is anyone.") but misses "Can I has pie?", "These are have", "This will be happened", and other subject-verb agreement errors that TextEdit highlights.
What I've confirmed:
- "Check Grammar With Spelling" is enabled in System Settings
- TextEdit reliably catches all these errors with green underlines
- Both APIs are called with a valid
spellDocumentTagfromuniqueSpellDocumentTag() - The text is passed as plain strings (no attributed string context)
My question: How does NSTextView's grammar checking work internally? It must be using something beyond these two public APIs. Possibilities I'm considering:
- Does NSTextView use the
NSTextCheckingClientprotocol /requestChecking(of:range:types:options:)for asynchronous checking that produces different results? - Does NSTextView provide additional context (attributed string, layout info) that improves grammar detection?
- Is there a private/undocumented API or framework that NSTextView uses for deeper grammar analysis?
Any insight from anyone who has implemented programmatic grammar checking on macOS would be appreciated.
NOTE: This post was composed with the help of Claude Code, which I am using to help write a word-processing application, but I am frustrated because Claude Code wants to give up and switch to a 3rd party grammar checker, like LanguageTool, and it seems to me that it should be possible to use native Apple tools to achieve this goal without requiring the user to send their data elsewhere for checking. I've spent a lot of time searching the web for answers and have found surprisingly little on this. Any pointers people might have would be very much appreciated! Thanks.