All skills
dpearson2699 avatar

/swiftui-webkit

@600737f

Embeds and controls web content in SwiftUI with WebKit for SwiftUI, including WebView, WebPage, navigation policies, JavaScript execution, observable page state, link interception, local HTML or data loading, and custom URL schemes. Use when building iOS 26+ article/detail views, help centers, in-app documentation, or other embedded web experiences backed by HTML, CSS, and JavaScript.

Use this Skill: https://skilld.dev/gh/dpearson2699/swift-ios-skills/swiftui-webkit

This session only. Nothing lands on disk.

referenceslocal-content-and-custom-schemes.md

≈708 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Local Content and Custom URL Schemes

Contents

  • When to use custom schemes
  • Registering a scheme handler
  • Returning responses and data
  • Loading bundled content
  • Cancellation behavior

When to use custom schemes

Use URLSchemeHandler when the app owns the content source and needs WebKit to resolve resources under a custom scheme.

Good fits:

  • bundled HTML, CSS, and JavaScript assets
  • offline documentation
  • app-owned rich content assembled on device

Do not use custom schemes for ordinary server-hosted pages that should just load over HTTPS.

Registering a scheme handler

import Foundation
import WebKit

@MainActor
func makeDocsPage() -> WebPage {
    var configuration = WebPage.Configuration()
    configuration.urlSchemeHandlers[URLScheme("docs")!] = DocsSchemeHandler(bundle: .main)
    return WebPage(configuration: configuration)
}

If WebKit already owns a scheme, URLScheme("https") style registration does not work. Use a genuinely custom scheme.

Returning responses and data

A handler replies with an async sequence of intermixed response and data values.

struct DocsSchemeHandler: URLSchemeHandler {
    let bundle: Bundle

    func reply(for request: URLRequest) -> some AsyncSequence<URLSchemeTaskResult> {
        AsyncStream { continuation in
            guard let url = request.url,
                  let fileURL = bundle.url(forResource: url.host, withExtension: "html", subdirectory: "Docs")
            else {
                continuation.finish()
                return
            }

            do {
                let data = try Data(contentsOf: fileURL)
                let response = URLResponse(
                    url: url,
                    mimeType: "text/html",
                    expectedContentLength: data.count,
                    textEncodingName: "utf-8"
                )
                continuation.yield(.response(response))
                continuation.yield(.data(data))
                continuation.finish()
            } catch {
                continuation.finish()
            }
        }
    }
}

Keep MIME type and encoding aligned with the actual content you serve.

Loading bundled content

Once registered, load the custom URL like any other page.

let page = makeDocsPage()
for try await _ in page.load(URL(string: "docs://welcome")!) {
}

This works well when the HTML references other assets with the same custom scheme.

Cancellation behavior

If WebKit no longer needs the resource, it cancels the task producing the async sequence. Treat cancellation as normal behavior when:

  • the user navigates away
  • the page reloads before the previous request completes
  • the resource is no longer needed by the page

Do not build logic that assumes every scheme-handled request runs to completion.

Source: SKILL.md on GitHub

No alerts17d4 checks · Risk SAFE
  • Gen Agent Trust Hub17d

    The skill provides comprehensive documentation and guidance for embedding web content in SwiftUI using native APIs. It promotes security best practices, such as using secure authentication sessions and avoiding unsafe JavaScript execution patterns. No security issues were detected.

  • Socket17d

    No alerts

  • Snyk17d

    Risk: LOW · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 600737f. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 days ago.

Steadyupdated 3 months ago

README badge

README badge for dpearson2699/swift-ios-skills/swiftui-webkit