Contents

init(url:)

Create a new WebView with the specified URL.

Declaration

@MainActor @preconcurrency init(url: URL?)

Parameters

  • url:

    The URL to display in the view. If this value is non-nil or changes to become a non-nil value, the new URL is loaded into the view.

Discussion

For example, you can create a WebView that displays one of two URLs depending on the state of a toggle:

struct URLView: View {
    @State private var url: URL? = nil
    @State private var toggle = false

    var body: some View {
        VStack {
            Button("Toggle") {
                toggle.toggle()
            }
            WebView(url: url)
        }
        .onChange(of: toggle, initial: true) {
            url = toggle ? URL(string: "https://www.webkit.org") : URL(string: "https://www.apple.com")
        }
    }
}

See Also

Creating web views