https://github.com/cybozu/webui
WebUI is a Swift package that provides WKWebView wrapped by SwiftUI.
https://github.com/cybozu/webui
ios macos swift swiftui
Last synced: about 1 year ago
JSON representation
WebUI is a Swift package that provides WKWebView wrapped by SwiftUI.
- Host: GitHub
- URL: https://github.com/cybozu/webui
- Owner: cybozu
- License: mit
- Created: 2024-04-17T07:31:19.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2025-04-02T10:10:29.000Z (about 1 year ago)
- Last Synced: 2025-04-02T21:43:24.103Z (about 1 year ago)
- Topics: ios, macos, swift, swiftui
- Language: Swift
- Homepage: https://cybozu.github.io/WebUI/documentation/webui/
- Size: 303 KB
- Stars: 117
- Watchers: 11
- Forks: 16
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README

WebUI is a Swift package that provides WKWebView wrapped by SwiftUI.
[](https://github.com/cybozu/WebUI/network/members)
[](https://github.com/cybozu/WebUI/stargazers)
[](https://github.com/cybozu/WebUI/issues)
[](https://github.com/cybozu/WebUI/releases)
[](https://github.com/cybozu/WebUI/blob/main/LICENSE)
## Requirements
- Development with Xcode 16.2+
- 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](https://cybozu.github.io/WebUI/documentation/webui/).
### Display Web Page
Use `WebView(request:)`.
```swift
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`.
```swift
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:)`.
```swift
struct ContentView: View {
let configuration: WKWebViewConfiguration
init() {
configuration = .init()
configuration.allowsInlineMediaPlayback = true
}
var body: some View {
WebView(configuration: configuration)
}
}
```
Other useful APIs are available.
```swift
struct ContentView: View {
var body: some View {
WebView()
.allowsLinkPreview(true)
.refreshable()
}
}
```
### Using with Delegates
Use `uiDelegate(_:)`, `navigationDelegate(_:)` method.
```swift
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)](https://cybozu.github.io/WebUI/documentation/webui/)
## Installation
WebUI is available through [Swift Package Manager](https://github.com/apple/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
// 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.
```sh
$ 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](/Examples/Examples.xcodeproj) and Run it.