WebUI is a Swift package that provides WKWebView wrapped by SwiftUI.
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.
- Development with Xcode 16.4+
- 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.
Contributions to WebUI are welcomed and encouraged! Please see the Contributing Guide.
This repository includes demonstration app for iOS & macOS.
Open Examples/Examples.xcodeproj and Run it.