Using Claude Code Hooks for Native macOS Swift Development
Using Claude Code Hooks for Native macOS Swift Development
Building a native macOS app in Swift with Claude Code, the single thing that changed my workflow the most was hooks. Not a new model, not a better prompt technique - hooks. The ability to run custom scripts before and after Claude Code actions transforms how you develop.
What Hooks Do
Claude Code hooks are scripts that execute automatically at specific points in the workflow. Before a file is saved, after a commit is created, when a new session starts - you define triggers and Claude Code runs your scripts at those moments. For Swift development, this is transformative.
The Game-Changing Hooks
Pre-save formatting. Every time Claude Code writes a Swift file, a hook runs swift-format on it before the save completes. No more inconsistent formatting across agent-generated code. Every file matches your project style, automatically.
Build validation. After any Swift file changes, a hook triggers xcodebuild to verify the project still compiles. Claude Code gets immediate feedback if its changes broke something, before you even look at the output. The agent can self-correct without you intervening.
Test runner. Before any commit, hooks run your test suite. If tests fail, the commit is blocked and Claude Code sees the failures. It fixes the issues and tries again. You get commits that always pass tests.
Session initialization. When a new Claude Code session starts, a hook reads your Xcode project structure and injects the current build target, scheme, and recent build errors into the context. The agent starts every session aware of your project state.
Why This Matters for Swift
Swift development has more ceremony than scripting languages. You need to manage Xcode project files, provisioning profiles, build schemes, and framework dependencies. Hooks automate the ceremony so you can focus on the logic.
Without hooks, you constantly interrupt Claude Code to say "now build it" or "run the tests." With hooks, these steps happen automatically. The development loop tightens from minutes to seconds.
Setting It Up
Define hooks in your .claude/hooks.json file. Start with just the build validation hook - it provides the most immediate value. Add formatting and testing hooks once the basic loop is working. Keep hook scripts fast - anything over a few seconds breaks the flow.
- Building a macOS AI Agent with Swift and ScreenCaptureKit
- Claude Code Swift ScreenCaptureKit and Deprecated APIs
- Native Swift AI Agent with Instant Launch
Fazm is an open source macOS AI agent. Open source on GitHub.