Stateful SwiftUI WebView for iOS and MacOS
Fully functional, SwiftUI-ready WebView for iOS 13+ and MacOS 10.15+. Actions and state are both delivered via SwiftUI @Binding
s, meaking it dead-easy to integrate into any existing SwiftUI View.
Installation
This component is distributed as a Swift package.
Sample usage
- The action binding is used to control the WebView - whichever action you want it to perform, just set the variable's value to it. Available actions:
idle
- does nothing and can be used as the default value.load(URLRequest)
- loads the given request.loadHTML(String)
- loads custom HTML string.reload
goBack
goForward
- The state binding reports back the current state of the WebView. Available data:
isLoading
-true
if the WebView is currently loading a page.pageTitle
- the title of the currently loaded page, ornil
if it can't be obtained.pageHTML
- the HTML code of the page content. SethtmlInState: true
inWebView
initializer to receive this update.error
- set if an error ocurred while loading the page,nil
otherwise.canGoBack
canGoForward
- The optional restrictedPages array allows you to specify hosts which the web view won't load.
- htmlInState dictates if the
state
update will containpageHTML
. This is disabled by default as it's a costly operation.
Recipe
For a more detailed description of the code, visit this recipe. Check out SwiftUIRecipes.com for more SwiftUI recipes!