OpenURLAction
An action that opens a URL.
Declaration
type OpenURLAction = { handler: { returns: OpenURLActionResult; perform?: (arg0: string) => void; }; };@MainActor @preconcurrency struct OpenURLActionType methods
Properties
handler*{ returns: OpenURLActionResult; perform?: (arg0: string) => void; }
Overview
Read the EnvironmentValues.openURL environment value to get an
instance of this structure for a given Environment. Call the
instance to open a URL. You call the instance directly because it
defines a 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 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 View.environment(_:_:)
view modifier. 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. Initialize an
action by calling the OpenURLAction.init(handler:) initializer with
a handler that takes a URL and returns an 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 OpenURLAction.Result.handled.
From Apple’s SwiftUI documentation. Code samples there are Swift.
See also
More in Navigation.
- AdaptableTabBarPlacementA placement for tabs in a tab view using the adaptable sidebar style.
- AutomaticNavigationTransitionA style that automatically chooses the appropriate presentation transition for the current context.
- ContentToolbarPlacementA region of the interface that hosts its own toolbar content.
- CrossFadeNavigationTransitionA navigation transition that cross-fades between the appearing view and the disappearing view.
- DismissSearchActionAn action that can end a search interaction.
- NavigationBarItemTitleDisplayModeA style for displaying the title of a navigation bar.
- NavigationPathA type-erased list of data representing the content of a navigation stack.
- NavigationPathChainNavigationPath with chained calls: SwiftUI.NavigationPath.append(...).
- NavigationPathCodableRepresentationA serializable representation of a navigation path.
- NavigationSplitViewColumnA view that represents a column in a navigation split view.
- NavigationSplitViewVisibilityThe visibility of the leading columns in a navigation split view.
- NavigationTransitionA type that defines the transition to use when navigating to a view.