Skip to content

cybozu/WebUI

Repository files navigation

WebUI by Cybozu

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

Github forks Github stars Github issues Github release Github license

Note

A native WebView for SwiftUI was officially announced by Apple at WWDC 25. However, as its minimum deployment target is iOS 26 and higher, WebUI continues to be an effective solution for projects that must support earlier versions of iOS.

Note

Xcode 16.4 has a bug where it cannot resolve the path for libswiftWebKit.dylib in the iOS Simulator.
A workaround is required to run apps in the iOS Simulator. Please see the release notes for details.
ref: https://developer.apple.com/forums/thread/785964

If you do not need the PDF output feature, or if you do not need to verify its functionality in the simulator, please use the workaround-for-xcode-16.4-16F6 branch.

Requirements

  • Development with Xcode 16.4+
  • 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.

Contributing to WebUI

Contributions to WebUI are welcomed and encouraged! Please see the Contributing Guide.

Demo

This repository includes demonstration app for iOS & macOS.

Open Examples/Examples.xcodeproj and Run it.

About

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

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Contributors 9

Languages