Agent skills for iOS, iPadOS, Swift, SwiftUI, and modern Apple framework development.
71
89%
Does it follow best practices?
Impact
—
No eval scenarios have been run
Advisory
Suggest reviewing before use
Use WebPage.NavigationDeciding when the app needs to allow only owned URLs inside the embedded page.
import Observation
import WebKit
@Observable
@MainActor
final class ArticleNavigationDecider: WebPage.NavigationDeciding {
var urlToOpenExternally: URL?
func decidePolicy(
for action: WebPage.NavigationAction,
preferences: inout WebPage.NavigationPreferences
) async -> WKNavigationActionPolicy {
guard let url = action.request.url else { return .allow }
if url.host == "docs.example.com" {
return .allow
}
urlToOpenExternally = url
return .cancel
}
}This keeps app-owned pages embedded while still letting the app hand off external destinations.
Bridge the decider back into SwiftUI with openURL.
struct ArticleView: View {
@Environment(\.openURL) private var openURL
@State private var decider: ArticleNavigationDecider
@State private var page: WebPage
init() {
let decider = ArticleNavigationDecider()
_decider = State(initialValue: decider)
_page = State(initialValue: WebPage(configuration: .init(), navigationDecider: decider))
}
var body: some View {
WebView(page)
.onChange(of: decider.urlToOpenExternally) { _, url in
guard let url else { return }
openURL(url)
decider.urlToOpenExternally = nil
}
}
}Use this for external links, legal pages, or routes that should leave the embedded surface.
callJavaScript executes an async JavaScript function and returns an optional Any.
let script = """
const headings = [...document.querySelectorAll('h1, h2')];
return headings.map(node => ({
id: node.id,
title: node.textContent?.trim()
}));
"""
let result = try await page.callJavaScript(script)
let headings = result as? [[String: Any]] ?? []Cast immediately into the specific structure the app expects.
Arguments become local variables inside the JavaScript function.
let topOffset = try await page.callJavaScript(
"return document.getElementById(sectionID)?.getBoundingClientRect().top ?? null;",
arguments: ["sectionID": selectedSectionID]
) as? DoubleThis is cleaner than interpolating untrusted values into the script string.
The native SwiftUI WebKit API clearly supports Swift-to-JavaScript calls, but it does not expose an obvious direct equivalent to WKScriptMessageHandler.
For coarse event handoff, a custom navigation pattern can work:
app-event://completed?id=123NavigationDeciding intercepts the URL.cancelUse this for simple completion or routing signals. Do not present it as a full structured messaging replacement for legacy WKUserContentController script handlers.
skills
accessorysetupkit
references
activitykit
references
adattributionkit
references
alarmkit
references
app-clips
app-intents
references
app-store-optimization
app-store-review
apple-on-device-ai
appmigrationkit
references
audioaccessorykit
references
authentication
references
avkit
references
background-processing
references
browserenginekit
references
callkit
references
carplay
references
cloudkit
references
contacts-framework
references
core-bluetooth
references
core-data
core-motion
references
core-nfc
references
coreml
references
cryptokit
references
cryptotokenkit
references
debugging-instruments
device-integrity
references
dockkit
references
energykit
references
eventkit
references
financekit
references
focus-engine
gamekit
references
healthkit
references
homekit
references
ios-accessibility
ios-localization
ios-networking
ios-simulator
references
mapkit
metrickit
references
musickit
references
natural-language
references
paperkit
references
passkit
references
pdfkit
references
pencilkit
references
permissionkit
references
photokit
push-notifications
realitykit
references
relevancekit
references
scenekit
references
sensorkit
references
speech-recognition
spritekit
references
storekit
swift-api-design-guidelines
swift-architecture
swift-charts
references
swift-codable
swift-concurrency
swift-formatstyle
swift-language
swift-security
references
swift-testing
swiftdata
swiftlint
swiftui-animation
swiftui-gestures
references
swiftui-layout-components
swiftui-liquid-glass
references
swiftui-patterns
swiftui-performance
swiftui-uikit-interop
swiftui-webkit
tabletopkit
references
tipkit
references
vision-framework
weatherkit
references
widgetkit
references