Contents

cybozu/WebUI

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

Requirements

  • Development with Xcode 26.0+
  • Written in Swift 6.1
  • 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.

<img src="https://github.com/user-attachments/assets/d332f31c-7cf8-42ba-9992-50a93110570b" width="800px">

  1. Add package and link WebUI to your application target.

<img src="https://github.com/user-attachments/assets/df726717-6ecc-4921-9997-1799e05927fc" width="600px">

CLI

  1. Create Package.swift that describes dependencies.

```swift // swift-tools-version: 6.1 import PackageDescription

let package = Package( name: "SomeProduct", products: [ .library(name: "SomeProduct", targets: ["SomeProduct"]) ], dependencies: [ .package(url: "https://github.com/cybozu/WebUI.git", exact: "4.0.0") ], targets: [ .target( name: "SomeProduct", dependencies: [ .product(name: "WebUI", package: "WebUI") ] ) ] ) ```

  1. Run the following command in Terminal.

``sh $ 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.

Package Metadata

Repository: cybozu/WebUI

Homepage: https://cybozu.github.io/WebUI/documentation/webui/

Stars: 170

Forks: 20

Open issues: 2

Default branch: main

Primary language: swift

License: MIT

Topics: ios, macos, swift, swiftui

README: README.md