WebUI

3.1.0

WebUI is a Swift package that provides WKWebView wrapped by SwiftUI.
cybozu/WebUI

What's New

3.1.0

2025-05-07T15:23:32Z

What's New

  • Added allowsOpaqueDrawing modifier to WebView.
  • Added contentSize and contentOffset properties to WebViewProxy.
  • Added contentReader which allows you to create PDF from current WebView's snapshot.
    WebViewReader { proxy in 
        VStack {
            Text("contentSize: \(proxy.contentSize)")
            Text("contentOffset: \(proxy.contentOffset)")
    
            Button {
                Task {
                    _ = try! await proxy.contentReader.pdf()
                }
            } label: {
                Image(systemImage: "printer")
            }
    
            WebView(request: URLRequest(url: URL(string: "https://example.com")!))
                .allowsOpaqueDrawing(false)
        }
    }

What's Changed

  • Migrated to Swift Testing by @Kyome22 in #45
  • Create CODEOWNERS by @Kyome22 in #46
  • Add allowsOpaqueDrawing(_:) modifier by @uhooi in #49
  • Put canImport(UIKit) to WebViewReaderTests by @Kyome22 in #53
  • Make allowsOpaqueDrawing work on macOS. by @Kyome22 in #52
  • Make it possible to refer to contentSize via WebViewProxy. by @Kyome22 in #54
  • minor fix about logo by @Kyome22 in #60
  • Added tests for WebViewProxy property binding. by @Kyome22 in #59
  • Support generating PDF from web view's content by @elmetal in #58

New Contributors

  • @uhooi made their first contribution in #49

Full Changelog: 3.0.3...3.1.0

WebUI by Cybozu

WebUI is a Swift package that provides WKWebView wrapped by SwiftUI.

Github forks Github stars Github issues Github release Github license

Requirements

  • Development with Xcode 16.2+
  • Written in Swift 6.0
  • Compatible with iOS 16.4+
  • Compatible with macOS 13.3+

Usage

Using WebUI, you can build a WebView in SwiftUI with simple APIs.

For more in-depth infomation, see API Documentation.

Display Web Page

Use WebView(request:).

struct ContentView: View {
    var body: some View {
        WebView(request: URLRequest(url: URL(string: "https://example.com/")!))
    }
}

Manipulating WebView

Use WebViewReader.Within the scope of WebViewReader, you can receive WebViewProxy.
You can manipulate WebView within the scope of WebViewReader via WebViewProxy.

struct ContentView: View {
    var body: some View {
        WebViewReader { proxy in
            WebView()
                .onAppear {
                    proxy.load(request: URLRequest(url: URL(string: "https://www.example.com")!))
                }

            Button("Reload") {
                proxy.reload()
            }
        }
        .padding()
    }
}

Customizing WebView

Use WebView(configuration:).

struct ContentView: View {
    let configuration: WKWebViewConfiguration

    init() {
        configuration = .init()
        configuration.allowsInlineMediaPlayback = true
    }

    var body: some View {
        WebView(configuration: configuration)
    }
}

Other useful APIs are available.

struct ContentView: View {
    var body: some View {
        WebView()
            .allowsLinkPreview(true)
            .refreshable()
    }
}

Using with Delegates

Use uiDelegate(_:), navigationDelegate(_:) method.

final class MyUIDelegate: NSObject, WKUIDelegate {}

final class MyNavigationDelegate: NSObject, WKNavigationDelegate {}

struct ContentView: View {
    var body: some View {
        WebView()
            .uiDelegate(MyUIDelegate())
            .navigationDelegate(MyNavigationDelegate())
    }
}

Documentation

Latest (Swift-DocC)

Installation

WebUI is available through Swift Package Manager.

Xcode

  1. File > Add Package Dependencies…
  2. Search https://github.com/cybozu/WebUI.git.
  3. Add package and link WebUI to your application target.

CLI

  1. Create Package.swift that describes dependencies.

    // swift-tools-version: 6.0
    import PackageDescription
    
    let package = Package(
        name: "SomeProduct",
        products: [
            .library(name: "SomeProduct", targets: ["SomeProduct"])
        ],
        dependencies: [
            .package(url: "https://github.com/cybozu/WebUI.git", exact: "3.0.0")
        ],
        targets: [
            .target(
                name: "SomeProduct",
                dependencies: [
                    .product(name: "WebUI", package: "WebUI")
                ]
            )
        ]
    )
  2. Run the following command in Terminal.

    $ swift package resolve

Privacy Manifest

This library does not collect or track user information, so it does not include a PrivacyInfo.xcprivacy file.

Demo

This repository includes demonstration app for iOS & macOS.

Open Examples/Examples.xcodeproj and Run it.

Description

  • Swift Tools 6.0.0
View More Packages from this Author

Dependencies

Last updated: Tue May 13 2025 14:25:03 GMT-0900 (Hawaii-Aleutian Daylight Time)