跳转至
Structure

OpenURLAction

Provides functionality for opening a URL.

Declaration

struct OpenURLAction

Overview

The OpenURLAction instance in the app's Environment offers a handler that you can use to open a URL in response to some action. Use the openURL environment value to get the handler. Then call the action's handler when you need to open a URL. For example, you can open a support URL in response to when a user taps a button:

struct OpenUrlActionView: View {
    @Environment(\.openURL) var openURL

    var body: some View {
        Button(action: contactSupport) {
            Text("Email Support")
            Image(systemName: "envelope.open")
        }
    }

    func contactSupport() {
        guard let url = URL(string: "https://www.example.com") else {
            return
        }
        openURL(url)
    }
}

Availability

iOS 14.0+

macOS 11.0+

tvOS 14.0+

watchOS 7.0+

Topics


Instance Method

callAsFunction(_:) Opens a URL, following system conventions.

callAsFunction(_:completion:) Asynchronously opens a URL, following system conventions.