WebUI is a Swift package that provides WKWebView wrapped by SwiftUI.
- Development with Xcode 16.0+
- Written in Swift 6.0
- Compatible with iOS 16.4+
- Compatible with macOS 13.3+
Using WebUI
, you can build a WebView in SwiftUI
with simple APIs.
For more in-depth infomation, see API Documentation.
Use WebView(request:)
.
struct ContentView: View {
var body: some View {
WebView(request: URLRequest(url: URL(string: "https://example.com/")!))
}
}
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()
}
}
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()
}
}
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())
}
}
WebUI is available through Swift Package Manager.
Xcode
- File > Add Package Dependencies…
- Search
https://github.com/cybozu/WebUI.git
.
- Add package and link
WebUI
to your application target.
CLI
- 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") ] ) ] )
- Run the following command in Terminal.
$ swift package resolve
This library does not collect or track user information, so it does not include a PrivacyInfo.xcprivacy file.
This repository includes demonstration app for iOS & macOS.
Open Examples/Examples.xcodeproj and Run it.