3

Circumstances

I've got a SwiftUI App with deep link support. When .onOpenUrl is triggered, the app navigates to a specific screen. Every view can listen to that deep link url and therefore has all information to properly set the relevant states (like the TabView selecting a specific tab). I use deep links for Widgets.

Now that my app supports push notifications, I want to use that deep link navigation handling to open a specific screen based on the push notifications userInfo that comes from the according UIApplicationDelegate method.

Idea

The idea is to generate a deep link url from that user info and pass it down the view hierarchy so that every view can react to .onOpenUrl and set state based on the opened push notification. That way I don't have to add an extra EnvironmentObject to the environment and listen to .onChange(of: someUserInfoInsideTheEnvironment) in every view, duplicating the existing .onOpenUrl handling for deep links.

Question

Is there a way to manually (programmatically) trigger .onOpenUrl with a specific url? Like setting a specific environment value or -object so that every view in the view hierarchy will call .onOpenUrl with my own deep link url?

1 Answer 1

0

You can use openURL(), just like you would for normal web links. I implemented this in my app with a custom widget url and it triggered onOpenURL() as expected.

Import openURL with:

@Environment(\.openURL) private var openURL 

Full details: https://developer.apple.com/documentation/swiftui/environmentvalues/openurl

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.