<!--
{
  "availability" : [
    "iOS: 14.0.0 -",
    "iPadOS: 14.0.0 -",
    "macCatalyst: 14.0.0 -",
    "macOS: 11.0.0 -",
    "tvOS: 14.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 7.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/EnvironmentValues/openURL",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Property",
  "symbol" : {
    "kind" : "Instance Property",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI17EnvironmentValuesV7openURLAA13OpenURLActionVvp"
  },
  "title" : "openURL"
}
-->

# openURL

An action that opens a URL.

```
@MainActor @preconcurrency var openURL: OpenURLAction { get set }
```

## Discussion

Read this environment value to get an [`OpenURLAction`](/documentation/SwiftUI/OpenURLAction)
instance for a given [`Environment`](/documentation/SwiftUI/Environment). Call the
instance to open a URL. You call the instance directly because it
defines a [`callAsFunction(_:)`](/documentation/SwiftUI/OpenURLAction/callAsFunction(_:)) method that Swift
calls when you call the instance.

For example, you can open a web site when the user taps a button:

```
struct OpenURLExample: View {
    @Environment(\.openURL) private var openURL

    var body: some View {
        Button {
            if let url = URL(string: "https://www.example.com") {
                openURL(url)
            }
        } label: {
            Label("Get Help", systemImage: "person.fill.questionmark")
        }
    }
}
```

If you want to know whether the action succeeds, add a completion
handler that takes a Boolean value. In this case, Swift implicitly
calls the [`callAsFunction(_:completion:)`](/documentation/SwiftUI/OpenURLAction/callAsFunction(_:completion:)) method
instead. That method calls your completion handler after it determines
whether it can open the URL, but possibly before it finishes opening
the URL. You can add a handler to the example above so that
it prints the outcome to the console:

```
openURL(url) { accepted in
    print(accepted ? "Success" : "Failure")
}
```

The system provides a default open URL action with behavior
that depends on the contents of the URL. For example, the default
action opens a Universal Link in the associated app if possible,
or in the user’s default web browser if not.

You can also set a custom action using the [`environment(_:_:)`](/documentation/SwiftUI/View/environment(_:_:))
view modifier. 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. Initialize an
action by calling the [`init(handler:)`](/documentation/SwiftUI/OpenURLAction/init(handler:)) initializer with
a handler that takes a URL and returns an [`OpenURLAction.Result`](/documentation/SwiftUI/OpenURLAction/Result):

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

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)