---
title: "init(url:)"
framework: webkit
role: symbol
role_heading: Initializer
path: "webkit/webview-swift.struct/init(url:)"
---

# init(url:)

Create a new WebView with the specified URL.

## Declaration

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

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

- [init(_:)](webkit/webview-swift.struct/init(_:).md)
