Skip to content
/ WebUI Public

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

License

Notifications You must be signed in to change notification settings

cybozu/WebUI

Repository files navigation

WebUI by Cybozu

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

Github issues Github forks Github stars Top language Release Github license

Requirements

  • Development with Xcode 15.4+
  • Written in Swift 5.10
  • 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: 5.10
    import PackageDescription
    
    let package = Package(
        name: "SomeProduct",
        products: [
            .library(name: "SomeProduct", targets: ["SomeProduct"])
        ],
        dependencies: [
            .package(url: "https://github.com/cybozu/WebUI.git", exact: "2.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.