webkit-integration

WebKit integration in SwiftUI using WebView and WebPage for embedding web content, navigation, JavaScript interop, and customization. Use when embedding web content in SwiftUI apps.

149 stars

Best use case

webkit-integration is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

WebKit integration in SwiftUI using WebView and WebPage for embedding web content, navigation, JavaScript interop, and customization. Use when embedding web content in SwiftUI apps.

Teams using webkit-integration should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/webkit/SKILL.md --create-dirs "https://raw.githubusercontent.com/rshankras/claude-code-apple-skills/main/skills/swiftui/webkit/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/webkit/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How webkit-integration Compares

Feature / Agentwebkit-integrationStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

WebKit integration in SwiftUI using WebView and WebPage for embedding web content, navigation, JavaScript interop, and customization. Use when embedding web content in SwiftUI apps.

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

Related Guides

SKILL.md Source

# WebKit Integration for SwiftUI

Embed and control web content in SwiftUI apps using the native `WebView` struct and `WebPage` observable class. Covers loading, navigation, JavaScript execution, and view customization.

## When This Skill Activates

- User wants to display web content inside a SwiftUI app
- User needs to load URLs, HTML strings, or data blobs in a web view
- User asks about JavaScript interop from SwiftUI
- User needs navigation control (back, forward, reload) for embedded web content
- User wants to customize web view behavior (gestures, text selection, link previews)
- User needs to capture snapshots or export PDFs from web content
- User asks about intercepting navigation requests or custom URL schemes
- User wants to configure private browsing or custom user agents

## Decision Tree

```
What do you need?
|
+-- Display a URL or HTML content
|   +-- Simple, no interaction needed
|   |   +-- WebView(url:) --> see webview-basics.md
|   +-- Need loading state, reload, custom config
|       +-- WebPage + WebView(page) --> see webview-basics.md
|
+-- Navigate programmatically (back, forward, intercept)
|   +-- Back/forward list, navigation events
|   |   +-- see navigation.md
|   +-- Intercept or cancel navigation requests
|       +-- NavigationDeciding protocol --> see navigation.md
|
+-- Execute JavaScript or communicate with web content
|   +-- callJavaScript, arguments, content worlds
|       +-- see javascript-advanced.md
|
+-- Capture snapshots, export PDF, web archive
|   +-- page.snapshot(), page.pdf(), page.webArchiveData()
|       +-- see javascript-advanced.md
|
+-- Handle custom URL schemes
    +-- URLSchemeHandler protocol --> see javascript-advanced.md
```

## API Availability

| API | Minimum OS | Import |
|-----|-----------|--------|
| `WebView` | iOS 26 / macOS 26 | `SwiftUI` + `WebKit` |
| `WebPage` | iOS 26 / macOS 26 | `WebKit` |
| `WebPage.Configuration` | iOS 26 / macOS 26 | `WebKit` |
| `NavigationDeciding` | iOS 26 / macOS 26 | `WebKit` |
| `WKContentWorld` | iOS 14 / macOS 11 | `WebKit` |
| `WKSnapshotConfiguration` | iOS 11 / macOS 10.13 | `WebKit` |
| `WKPDFConfiguration` | iOS 14 / macOS 11 | `WebKit` |

## Quick Start

### Simplest Usage

```swift
import SwiftUI
import WebKit

struct BrowserView: View {
    var body: some View {
        WebView(url: URL(string: "https://developer.apple.com")!)
    }
}
```

### With WebPage for Full Control

```swift
import SwiftUI
import WebKit

struct ControlledBrowserView: View {
    @State private var page = WebPage()

    var body: some View {
        WebView(page)
            .onAppear {
                page.load(URLRequest(url: URL(string: "https://developer.apple.com")!))
            }
    }
}
```

## Top Mistakes

| Mistake | Problem | Fix |
|---------|---------|-----|
| Using `WebView(url:)` when you need navigation control | No access to back/forward, reload, or events | Use `WebPage` + `WebView(page)` |
| Forgetting `import WebKit` alongside `import SwiftUI` | `WebView` is in SwiftUI but `WebPage` requires WebKit | Always import both |
| Not observing `currentNavigationEvent` | Missing loading states, errors go unnoticed | Use `onChange(of: page.currentNavigationEvent)` |
| Calling `callJavaScript` before page finishes loading | Script fails because DOM is not ready | Wait for `.finished` navigation event |
| Using persistent data store for private browsing | User data is saved to disk | Use `.nonPersistent()` on `WebsiteDataStore` |
| Not handling `nil` return from `decidePolicyFor(navigationAction:)` | Navigation proceeds when it should be cancelled | Return `nil` to cancel, return `NavigationPreferences` to allow |
| Passing JavaScript without argument binding | Vulnerable to injection, hard to debug | Use `arguments:` parameter for named values |

## Review Checklist

- [ ] Using `WebPage` when any control beyond simple display is needed
- [ ] Both `SwiftUI` and `WebKit` are imported
- [ ] Navigation events observed for loading indicators and error handling
- [ ] JavaScript execution waits for page to finish loading
- [ ] Private browsing uses `.nonPersistent()` data store
- [ ] Navigation interception returns correct values (preferences to allow, nil to cancel)
- [ ] JavaScript arguments passed via `arguments:` parameter, not string interpolation
- [ ] Custom URL scheme handler registered on configuration before page loads
- [ ] Find-in-page enabled with `findNavigator(isPresented:)` if needed
- [ ] Appropriate gesture and interaction modifiers applied (back/forward, magnification, text selection)
- [ ] Content background customized if needed for visual integration

## Reference Files

| File | Contents |
|------|----------|
| `webview-basics.md` | WebView creation, WebPage setup, configuration, find-in-page, customization modifiers |
| `navigation.md` | Loading content, back/forward list, navigation events, NavigationDeciding protocol |
| `javascript-advanced.md` | JavaScript execution, content worlds, snapshots, PDF export, custom URL schemes |

## Cross-References

- For **macOS window management** around web views, see `macos/architecture-patterns/`
- For **navigation architecture** that hosts a web view, see `ios/navigation-patterns/`
- For **Liquid Glass** design around web content, see `design/liquid-glass/`

## References

- [WebView (SwiftUI)](https://developer.apple.com/documentation/swiftui/webview)
- [WebPage](https://developer.apple.com/documentation/webkit/webpage)
- [WKContentWorld](https://developer.apple.com/documentation/webkit/wkcontentworld)
- [WKSnapshotConfiguration](https://developer.apple.com/documentation/webkit/wksnapshotconfiguration)

Related Skills

integration-test-scaffold

149
from rshankras/claude-code-apple-skills

Generate cross-module test harness with mock servers, in-memory stores, and test configuration. Use when testing networking + persistence + business logic together.

watchOS

149
from rshankras/claude-code-apple-skills

watchOS development guidance including SwiftUI for Watch, Watch Connectivity, complications, and watch-specific UI patterns. Use for watchOS code review, best practices, or Watch app development.

visionos-widgets

149
from rshankras/claude-code-apple-skills

visionOS widget patterns including mounting styles, glass/paper textures, proximity-aware layouts, and spatial widget families. Use when creating or adapting widgets for visionOS.

test-data-factory

149
from rshankras/claude-code-apple-skills

Generate test fixture factories for your models. Builder pattern and static factories for zero-boilerplate test data. Use when tests need sample data setup.

test-contract

149
from rshankras/claude-code-apple-skills

Generate protocol/interface test suites that any implementation must pass. Define the contract once, test every implementation. Use when designing protocols or swapping implementations.

tdd-refactor-guard

149
from rshankras/claude-code-apple-skills

Pre-refactor safety checklist. Verifies test coverage exists before AI modifies existing code. Use before asking AI to refactor anything.

tdd-feature

149
from rshankras/claude-code-apple-skills

Red-green-refactor scaffold for building new features with TDD. Write failing tests first, then implement to pass. Use when building new features test-first.

tdd-bug-fix

149
from rshankras/claude-code-apple-skills

Fix bugs using red-green-refactor — reproduce the bug as a failing test first, then fix it. Use when fixing bugs to ensure they never regress.

snapshot-test-setup

149
from rshankras/claude-code-apple-skills

Set up SwiftUI visual regression testing with swift-snapshot-testing. Generates snapshot test boilerplate and CI configuration. Use for UI regression prevention.

characterization-test-generator

149
from rshankras/claude-code-apple-skills

Generates tests that capture current behavior of existing code before refactoring. Use when you need a safety net before AI-assisted refactoring or modifying legacy code.

testing

149
from rshankras/claude-code-apple-skills

TDD and testing skills for iOS/macOS apps. Covers characterization tests, TDD workflows, test contracts, snapshot tests, and test infrastructure. Use for test-driven development, adding tests to existing code, or building test infrastructure.

toolbars

149
from rshankras/claude-code-apple-skills

Modern SwiftUI toolbar patterns including customizable toolbars, search integration, transition effects, and platform-specific behavior. Use when implementing or customizing toolbars in SwiftUI.