TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: .onOpenURL not being called

Forums > SwiftUI

I have a legacy app which I'm attempting to convert to SwiftUI. Things are all going fantasticly well, except for deep link handling - the links do open the app, but they don't perform any action. Here's my App structure:

import SwiftUI

@main
struct RedactedApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) private var delegate
    @Environment(\.scenePhase) var scenePhase

    init() {
        AppManager.initialize()
    }

    var body: some Scene {
        WindowGroup {
            ContentView()
                .onOpenURL { url in
                    print("onOpenURL - \(url)") //or the real logic but I don't hit the break point or see this print when I open a link
                }
        }
        .onChange(of: scenePhase) { newScenePhase in
            switch newScenePhase {
            case .active:
                AppManager.appDidBecomeActive()
            case .inactive:
                AppManager.appWillResignActive()
            case .background:
                AppManager.appBackgrounded()
            @unknown default:
                break
            }
        }
    }
}

We're using @UIApplicationDelegateAdaptor because we also need to receive remote push notifications (not what we're testing here - this is just straight deep links tested with QR codes). I removed the old deep link methods from AppDelegate, but also tried leaving those and getting rid of .onOpenURL, and running both together - nothing is hitting break points or taking any actions.

Anyone have any experience with this or see anything wrong with this code?

3      

Figured it out! (Well, mostly.) It works if I use .onContinueUserActivity(NSUserActivityTypeBrowsingWeb, perform: {}) instead of .onOpenUrl. I don't know why exactly; we are using Universal Links so it may be related to that.

Anyway, just closing this out for future Googlers.

3      

Wanted to come back and add further clarification - .onOpenUrl WAS working for non-Web link sources (e.g., those external push notifications), but wasn't for anything coming from Web (including QR codes). We now have both .onContinueUserActivity and .onOpenUrl and all scenarios seem to be handled correctly. I don't understand why external Web sources aren't hitting .onOpenUrl, but running both seems to be working fine.

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.