Swiftpack.co - kylehickinson/SwiftUI-WebView as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by kylehickinson.
kylehickinson/SwiftUI-WebView 0.3.0
A SwiftUI component to use WKWebView
⭐️ 241
🕓 3 days ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/kylehickinson/SwiftUI-WebView.git", from: "0.3.0")

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:

import SwiftUI
import WebView

struct 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()
  }
}

GitHub

link
Stars: 241
Last commit: 3 days ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Related Packages

Release Notes

0.3.0
1 year ago
  • Adds macOS Support (#2)
  • Adds @dynamicMemberLookup to WebViewStore for a shortcut to the underlying WKWebView's properties
  • Adds license (#1)
  • Removes unnecessary view container in UI/NS view representables

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics