SwiftUI External Events without SwiftUI Lifecycle

I am using the new "pure" SwiftUI App and Scene structs instead of an AppDelegate.

Some of my views accept custom url schemes and user activities using onOpenURL(perform:). Everything works as expected.

However, starting with Beta 6, Xcodes gives the following runtime warning:

runtime: SwiftUI: Cannot use Scene methods for URL, NSUserActivity, and other External Events without using SwiftUI Lifecycle. Without SwiftUI Lifecycle, advertising and handling External Events wastes resources, and will have unpredictable results.

What exactly am I doing wrong? What is *SwiftUI Lifecycle* referring to?
it appears to with the new xcode12 and ios 14
Any solution found for this?
Please post your solution if found.
also seeing this in Xcode 12.1 (12A7403) with ios 14.1
ok after weeks of getting nowhere on this, I found the cause.

For me this was caused by having multiple .onOpenURL's similar to you. I had one in each view that I wanted to action the url. While it worked most times, this was causing unpredictable results intermittently for my app and gave this error each time.

So I moved the .onOpenURL to the App file then used an environment variable to pass the url to the views I wanted to action it.

something like this:
Code Block
struct MyApp: App {
    @State var deepLink: URL? = nil
    var body: some Scene {
        WindowGroup {
            ContentView()
                .environment(\.deepLink, self.deepLink)
                .onOpenURL { url in
                    guard url.scheme == "myApp" else { return }
                    self.deepLink = url
/* clear the environment variable so you can send the same url twice */
                    DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(200), execute: {
                        self.deepLink = nil
                    })
                }
        }
    }
}

remember to setup your environment variables
Code Block
import SwiftUI
struct deepLinkKey: EnvironmentKey {
    static var defaultValue: URL? = nil
}
extension EnvironmentValues {
    var deepLink: URL? {
        get { self[deepLinkKey.self] }
        set { self[deepLinkKey.self] = newValue }
    }
}

then in the views u want to use it in
Code Block
var body: some View {
content()
.onChange(of:deepLink, perform: { url in
guard url?.scheme == "myApp" else { return }
             /* parse the url here*/
})
}

btw thanks for mentioning onOpenURL, otherwise I would never have found it.


Yeah, I just ran into this (or at least, I think it's the same problem). You can also make the environmental URL a binding, so you can clear it in the onChange(of:) handler.
SwiftUI External Events without SwiftUI Lifecycle
 
 
Q