0

I have a pretty easy code solution, that opens the app as it should. The Associated Domains and the apple-app-site-association file all work, but if I have a variable in there, or even a print statement, it won't get called. To be even more weird, it does all the things expected on the simulator using RocketSim's DeepLink feature.

So with everything setup and this small code, the app opens perfectly, except the debugPrint statements or the .toggle, only in the simulator:

struct ContentView<ViewModel: ContentViewModelProtocol>: View {
    @StateObject private var viewModel: ViewModel

    init(viewModel: @autoclosure @escaping () -> ViewModel = ContentViewModel()) {
        self._viewModel = StateObject(wrappedValue: viewModel())
    }

    var body {
        // just some basic view structure

        .onOpenURL { url in
            debugPrint("Received URL onOpenUrl: \(url)")
            handleUniversalLink(url: url)
        }
    }
    
    private func handleUniversalLink(url: URL) {
        debugPrint("Received URL handleUniversalLink: \(url)")
        
        let path = url.path
        
        if path.starts(with: "/test") {
            viewModel.isSheetPresented.toggle()
        }
    }
}

protocol ContentViewModelProtocol: ObservableObject {
    var isSheetPresented: Bool { get set }
}

class ContentViewModel {
    @Published var isSheetPresented = false
}

I have put this code in the @Main, on the ContentView, doesn't care. It just doesn't give me any information using it with an actual device.

If I have a sepcific tab switch based on the url inside handleUniversalLink, somehow it works, but a toggle or a print statement don't.

What am I doing wrong here?

3
  • Maybe unrelated, but have you tried using @StateObject private var viewModel = ViewModel() instead of the mumbo jumbo init() Commented Jul 8 at 12:20
  • @workingdogsupportUkraine I mean its just basic dependcy injection, everything else works in the app with this, if it was just this, then at least the print statements should be seen in the console, which dont
    – bennyyy999
    Commented Jul 8 at 13:02
  • You could try .environment(\.openURL, OpenURLAction... instead of .onOpenURL. Note (unrelated), you should declare class ContentViewModel: ContentViewModelProtocol .... If you don't see the debugPrint, it's probably because the flow does not go where you think it is going. Could you show some (minimal but functional) code we can compile and run. Commented Jul 9 at 0:09

1 Answer 1

0

The answer seems to be this one: https://forums.developer.apple.com/forums/thread/674141

Via a link (browser) it just worked and camera needed the other function from the apple forum:

.onContinueUserActivity(NSUserActivityTypeBrowsingWeb) { userActivity in
        print("Continue activity \(userActivity)")
        guard let url = userActivity.webpageURL else {
                return
        }
        print("User wants to open URL: \(url)")
        // TODO same handling as done in onOpenURL()
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.