swiftui-webkit
Embed and control web content in SwiftUI apps using WebView and WebPage. Use when displaying URLs or HTML in a SwiftUI app, implementing JavaScript interop, controlling web navigation, capturing snapshots, or handling custom URL schemes. Core APIs require iOS/macOS 26+.
- License 许可
- MIT
- 最近更新
- 2026-11-13
- Stars
- ★ 12
- Forks
- 0
01 前言 Front Matter
- name
- swiftui-webkit
- metadata
- { "author": "rshankras" }
- description
- Embed and control web content in SwiftUI apps using WebView and WebPage. Use when displaying URLs or HTML in a SwiftUI app, implementing JavaScript interop, controlling web navigation, capturing snapshots, or handling custom URL schemes. Core APIs require iOS/macOS 26+.
03 SKILL.md 正文
WebKit Integration for SwiftUI
Embed and control web content in SwiftUI using WebView and WebPage.
When This Skill Activates
Use this skill when the user needs to:
- Display web content within SwiftUI applications
- Load URLs, HTML strings, or data blobs
- Execute JavaScript from SwiftUI
- Control navigation (back, forward, reload)
- Customize web view behavior
- Capture snapshots or generate PDFs
- Intercept navigation requests
- Configure private browsing or custom user agents
API Availability
| API | Minimum Version | Notes |
|---|---|---|
WebView |
iOS/macOS 26 | Main SwiftUI web view |
WebPage |
iOS/macOS 26 | Observable web page controller |
WebPage.Configuration |
iOS/macOS 26 | Configuration object |
WKContentWorld |
iOS 14 / macOS 11 | JavaScript execution worlds |
WKSnapshotConfiguration |
iOS 11 / macOS 10.13 | Snapshot capture |
Quick Start
Minimal — Just a URL
import SwiftUI
import WebKit
struct SimpleWebView: View {
var body: some View {
WebView(url: URL(string: "https://apple.com")!)
}
}
Full Control — with WebPage
import SwiftUI
import WebKit
struct ControlledWebView: View {
@State private var page = WebPage()
var body: some View {
VStack {
HStack {
Button("Back") { page.goBack() }
.disabled(!page.canGoBack)
Button("Forward") { page.goForward() }
.disabled(!page.canGoForward)
Button("Reload") { page.reload() }
}
WebView(page)
}
.onAppear {
page.load(URLRequest(url: URL(string: "https://apple.com")!))
}
}
}
JavaScript Execution
// Execute after page loads
let result = try await page.callJavaScript("document.title")
// Inject script
await page.evaluateJavaScript("document.body.style.backgroundColor = 'red'")
Wait for page load before executing JavaScript — check page.isLoading or observe load events.
Navigation Control
page.load(URLRequest(url: url))
page.loadHTMLString("<h1>Hello</h1>", baseURL: nil)
page.goBack()
page.goForward()
page.reload()
page.stopLoading()
Critical Mistakes to Avoid
| # | Mistake | Fix |
|---|---|---|
| 1 | Using WebView(url:) when you need navigation control |
Use WebPage for any interactivity |
| 2 | Missing import WebKit |
Required alongside import SwiftUI |
| 3 | Not observing navigation events | Observe page.isLoading and navigation callbacks |
| 4 | Executing JavaScript before DOM is ready | Wait for page load completion |
| 5 | Using persistent data store in private mode | Use ephemeral data store for private browsing |
| 6 | String interpolation in JavaScript | Sanitize all user input before injecting into JS |
Review Checklist
- [ ]
import WebKitpresent alongsideimport SwiftUI - [ ] Using
WebPagefor any navigation or JavaScript needs - [ ] JavaScript execution waits for page load
- [ ] User input sanitized before JavaScript injection
- [ ] Navigation events observed for loading states
- [ ] iOS/macOS 26+ requirement documented
04 同仓库更多 Skills
youtube
Download YouTube video transcripts when user provides a YouTube URL or asks to download/get/fetch a transcript from YouTube. Also use when user wants to transcribe or get captions/subtitles from a YouTube video.
write-plan
Use when you have a spec or requirements for a multi-step task, before touching code. Produces an engineering plan complete with TDD tasks, DRY/SOLID/YAGNI principles, a required Test Plan & Verification section, and a coverage target — then auto-runs /plan-eng-review as an independent double-check before execution.
worktrees
Use when starting feature work that needs isolation from current workspace or before executing implementation plans - creates isolated git worktrees with smart directory selection and safety verification
web-perf
Analyzes web performance using Chrome DevTools MCP. Measures Core Web Vitals (LCP, INP, CLS) and supplementary metrics (FCP, TBT, Speed Index), identifies render-blocking resources, network dependency chains, layout shifts, caching issues, and accessibility gaps. Use when asked to audit, profile, debug, or optimize page load performance, Lighthouse scores, or site speed. Biases towards retrieval from current documentation over pre-trained knowledge.
verify
Use when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions always
validate-idea
Validate a business idea using the minimalist entrepreneur framework. Use when someone has a business idea and wants to test if it's worth pursuing before building anything.