Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kylehickinson/SwiftUI-WebView
A SwiftUI component to use WKWebView
https://github.com/kylehickinson/SwiftUI-WebView
swiftui webview wkwebview
Last synced: 2 months ago
JSON representation
A SwiftUI component to use WKWebView
- Host: GitHub
- URL: https://github.com/kylehickinson/SwiftUI-WebView
- Owner: kylehickinson
- License: unlicense
- Created: 2019-07-01T18:49:21.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-04-02T20:40:38.000Z (9 months ago)
- Last Synced: 2024-10-19T22:33:59.523Z (3 months ago)
- Topics: swiftui, webview, wkwebview
- Language: Swift
- Homepage:
- Size: 12.7 KB
- Stars: 340
- Watchers: 8
- Forks: 45
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# WebView
A SwiftUI component `View` that contains a `WKWebView`
Since `WKWebView` handles a lot of its own state, navigation stack, etc, it's almost easier to treat it as a mutable data model. You can set it up prior to how you need it, and then simply use its data within your SwiftUI View's.
Simply spin up a `WebViewStore` (optionally with your own `WKWebView`) and use that to access the `WKWebView` itself as if it was a data model.
Example usage:
```swift
import SwiftUI
import WebViewstruct ContentView: View {
@StateObject var webViewStore = WebViewStore()
var body: some View {
NavigationView {
WebView(webView: webViewStore.webView)
.navigationBarTitle(Text(verbatim: webViewStore.title ?? ""), displayMode: .inline)
.navigationBarItems(trailing: HStack {
Button(action: goBack) {
Image(systemName: "chevron.left")
.imageScale(.large)
.aspectRatio(contentMode: .fit)
.frame(width: 32, height: 32)
}.disabled(!webViewStore.canGoBack)
Button(action: goForward) {
Image(systemName: "chevron.right")
.imageScale(.large)
.aspectRatio(contentMode: .fit)
.frame(width: 32, height: 32)
}.disabled(!webViewStore.canGoForward)
})
}.onAppear {
self.webViewStore.webView.load(URLRequest(url: URL(string: "https://apple.com")!))
}
}
func goBack() {
webViewStore.webView.goBack()
}
func goForward() {
webViewStore.webView.goForward()
}
}
```