<!--
{
  "availability" : [
    "iOS: 26.0.0 -",
    "iPadOS: 26.0.0 -",
    "macCatalyst: -",
    "macOS: 26.0.0 -",
    "visionOS: 26.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "WebKit",
  "identifier" : "/documentation/WebKit/WebView-swift.struct/init(url:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "WebKit",
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:15_WebKit_SwiftUI0A4ViewV3urlAC10Foundation3URLVSg_tcfc"
  },
  "title" : "init(url:)"
}
-->

# init(url:)

Create a new WebView with the specified URL.

```
@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:

```swift
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")
        }
    }
}
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)