Andrew Zheng
2 min readJun 25, 2020

--

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.

Left: when launched from a link. Right: when launched directly from the home screen.

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:

Source from Koraktor’s great Stack Overflow answer

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.

--

--

Andrew Zheng

WWDC21 Scholar. I like to read manga and write about Swift. Check out my app, Find — look for text in real life, with outrageous speed. getfind.app