Initializer

init(handler:)

Creates an action that opens a URL.

Declaration 宣言

init(handler: @escaping (URL) -> OpenURLAction.Result)

Parameters パラメータ

handler

The closure to run for the given URL. The closure takes a URL as input, and returns a OpenURLAction.Result that indicates the outcome of the action.

Discussion 議論

Use this initializer to create a custom action for opening URLs. Provide a handler that takes a URL and returns an OpenURLAction.Result. Place your handler in the environment using the environment(_:_:) view modifier:


Text("Visit [Example Company](https://www.example.com) for details.")
    .environment(\.openURL, OpenURLAction { url in
        handleURL(url) // Define this method to take appropriate action.
        return .handled
    })

Any views that read the action from the environment, including the built-in Link view and Text views with markdown links, or links in attributed strings, use your action.

SwiftUI translates the value that your custom action’s handler returns into an appropriate Boolean result for the action call. For example, a view that uses the action declared above receives true when calling the action, because the handler always returns handled.

See Also 参照

Creating the Action