Great question!
First, how to get the link that the app was launched from, if any.
Answer: Inside your App
struct, implement the .onOpenURL
view modifier on ContentView
:
Here, we have a @State
property, which is synced with ContentView
using @Binding
(I go in depth about that here).
This is what ContentView looks like:
It displays a Text
that shows the link (I used a URL scheme, called helloworld
) that the app was launched from. And if the app was not launched from a link, it displays the placeholder.
Because a @State
property updates automatically, .onOpenURL
is able to replace both of the functions from SceneDelegate — in SwiftUI, you don’t need a separate function for loading the url on first launch, and another one for loading the url after coming back from the background.
You can get more information about .onOpenUrl
here.
Second, how to monitor device rotation
Answer: There’s a lot of ways to do this, but the easiest is to use a notification publisher. Here’s what ContentView will look like:
Result:
Basically, we’re combining Foundation
’s Notification Center with Combine
’s Publisher (Foundation
and Combine
are frameworks). You can check this out for more information on Notification Publishers.