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
import SwiftUI
import WebKit
struct BrowserView: View {
var body: some View {
WebView(url: URL(string: "https://developer.apple.com")!)
}
}
With WebPage for Full Control
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
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
1---2name: webkit-integration3description: 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.4---5
6# WebKit Integration for SwiftUI
7
8Embed and control web content in SwiftUI apps using the native `WebView` struct and `WebPage` observable class. Covers loading, navigation, JavaScript execution, and view customization.
9
10## When This Skill Activates
11
12- User wants to display web content inside a SwiftUI app
13- User needs to load URLs, HTML strings, or data blobs in a web view
14- User asks about JavaScript interop from SwiftUI
15- User needs navigation control (back, forward, reload) for embedded web content
16- User wants to customize web view behavior (gestures, text selection, link previews)
17- User needs to capture snapshots or export PDFs from web content
18- User asks about intercepting navigation requests or custom URL schemes
19- User wants to configure private browsing or custom user agents
20
21## Decision Tree
22
23```
24What do you need?
25|
26+-- Display a URL or HTML content
27| +-- Simple, no interaction needed
28| | +-- WebView(url:) --> see webview-basics.md
29| +-- Need loading state, reload, custom config
30| +-- WebPage + WebView(page) --> see webview-basics.md
31|
32+-- Navigate programmatically (back, forward, intercept)
33| +-- Back/forward list, navigation events
34| | +-- see navigation.md
35| +-- Intercept or cancel navigation requests
36| +-- NavigationDeciding protocol --> see navigation.md
37|
38+-- Execute JavaScript or communicate with web content
39| +-- callJavaScript, arguments, content worlds
40| +-- see javascript-advanced.md
41|
42+-- Capture snapshots, export PDF, web archive
43| +-- page.snapshot(), page.pdf(), page.webArchiveData()
44| +-- see javascript-advanced.md
45|
46+-- Handle custom URL schemes
47 +-- URLSchemeHandler protocol --> see javascript-advanced.md
48```
49
50## API Availability
51
52| API | Minimum OS | Import |
53|-----|-----------|--------|
54| `WebView` | iOS 26 / macOS 26 | `SwiftUI` + `WebKit` |
55| `WebPage` | iOS 26 / macOS 26 | `WebKit` |
56| `WebPage.Configuration` | iOS 26 / macOS 26 | `WebKit` |
57| `NavigationDeciding` | iOS 26 / macOS 26 | `WebKit` |
58| `WKContentWorld` | iOS 14 / macOS 11 | `WebKit` |
59| `WKSnapshotConfiguration` | iOS 11 / macOS 10.13 | `WebKit` |
60| `WKPDFConfiguration` | iOS 14 / macOS 11 | `WebKit` |
61
62## Quick Start
63
64### Simplest Usage
65
66```swift
67import SwiftUI
68import WebKit
69
70struct BrowserView: View {
71 var body: some View {
72 WebView(url: URL(string: "https://developer.apple.com")!)
73 }
74}
75```
76
77### With WebPage for Full Control
78
79```swift
80import SwiftUI
81import WebKit
82
83struct ControlledBrowserView: View {
84 @State private var page = WebPage()
85
86 var body: some View {
87 WebView(page)
88 .onAppear {
89 page.load(URLRequest(url: URL(string: "https://developer.apple.com")!))
90 }
91 }
92}
93```
94
95## Top Mistakes
96
97| Mistake | Problem | Fix |
98|---------|---------|-----|
99| Using `WebView(url:)` when you need navigation control | No access to back/forward, reload, or events | Use `WebPage` + `WebView(page)` |
100| Forgetting `import WebKit` alongside `import SwiftUI` | `WebView` is in SwiftUI but `WebPage` requires WebKit | Always import both |
101| Not observing `currentNavigationEvent` | Missing loading states, errors go unnoticed | Use `onChange(of: page.currentNavigationEvent)` |
102| Calling `callJavaScript` before page finishes loading | Script fails because DOM is not ready | Wait for `.finished` navigation event |
103| Using persistent data store for private browsing | User data is saved to disk | Use `.nonPersistent()` on `WebsiteDataStore` |
104| Not handling `nil` return from `decidePolicyFor(navigationAction:)` | Navigation proceeds when it should be cancelled | Return `nil` to cancel, return `NavigationPreferences` to allow |
105| Passing JavaScript without argument binding | Vulnerable to injection, hard to debug | Use `arguments:` parameter for named values |
106
107## Review Checklist
108
109- [ ] Using `WebPage` when any control beyond simple display is needed
110- [ ] Both `SwiftUI` and `WebKit` are imported
111- [ ] Navigation events observed for loading indicators and error handling
112- [ ] JavaScript execution waits for page to finish loading
113- [ ] Private browsing uses `.nonPersistent()` data store
114- [ ] Navigation interception returns correct values (preferences to allow, nil to cancel)
115- [ ] JavaScript arguments passed via `arguments:` parameter, not string interpolation
116- [ ] Custom URL scheme handler registered on configuration before page loads
117- [ ] Find-in-page enabled with `findNavigator(isPresented:)` if needed
118- [ ] Appropriate gesture and interaction modifiers applied (back/forward, magnification, text selection)
119- [ ] Content background customized if needed for visual integration
120
121## Reference Files
122
123| File | Contents |
124|------|----------|
125| `webview-basics.md` | WebView creation, WebPage setup, configuration, find-in-page, customization modifiers |
126| `navigation.md` | Loading content, back/forward list, navigation events, NavigationDeciding protocol |
127| `javascript-advanced.md` | JavaScript execution, content worlds, snapshots, PDF export, custom URL schemes |
128
129## Cross-References
130
131- For **macOS window management** around web views, see `macos/architecture-patterns/`
132- For **navigation architecture** that hosts a web view, see `ios/navigation-patterns/`
133- For **Liquid Glass** design around web content, see `design/liquid-glass/`
134
135## References
136
137- [WebView (SwiftUI)](https://developer.apple.com/documentation/swiftui/webview)
138- [WebPage](https://developer.apple.com/documentation/webkit/webpage)
139- [WKContentWorld](https://developer.apple.com/documentation/webkit/wkcontentworld)
140- [WKSnapshotConfiguration](https://developer.apple.com/documentation/webkit/wksnapshotconfiguration)