<!--
{
  "availability" : [
    "iOS: 15.0.0 -",
    "iPadOS: 15.0.0 -",
    "macCatalyst: 15.0.0 -",
    "macOS: 12.0.0 -",
    "tvOS: 15.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 8.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/OpenURLAction/init(handler:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI13OpenURLActionV7handlerA2C6ResultV10Foundation3URLVc_tcfc"
  },
  "title" : "init(handler:)"
}
-->

# init(handler:)

Creates an action that opens a URL.

```
@MainActor @preconcurrency 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`](/documentation/SwiftUI/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`](/documentation/SwiftUI/OpenURLAction/Result). Place your handler in the environment
using the [`environment(_:_:)`](/documentation/SwiftUI/View/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`](/documentation/SwiftUI/Link) view and [`Text`](/documentation/SwiftUI/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`](/documentation/SwiftUI/OpenURLAction/Result/handled).

---

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)