Wednesday, July 6, 2022
HomeiOS Developmentios - onChange not detecting AppDelegate Replace in SwiftUI

ios – onChange not detecting AppDelegate Replace in SwiftUI

I’ve an AppDelegate in my SwiftUI app, and I set a ViewModel like so:

class AppDelegate: NSObject, UIApplicationDelegate, ObservableObject {
    var AppNavigator: ApplicationNavigator = ApplicationNavigator()

That is my ApplicationNavigator View Mannequin:

class ApplicationNavigator: ObservableObject {
    @Printed var applicationNavigation: ApplicationNavigatorItem = ApplicationNavigatorItem(id: "default")

The place ApplicationNavigator takes an ApplicationNavigatorItem:

struct ApplicationNavigatorItem: Identifiable, Codable  {
    var id: String? = UUID().uuidString

And when a consumer faucets on a distant notification, I replace the AppNavigator in my AppDelegate with a brand new ApplicationNavigatorItem:

func userNotificationCenter(_ middle: UNUserNotificationCenter,
                              didReceive response: UNNotificationResponse,
                              withCompletionHandler completionHandler: @escaping () -> Void) {

    self.AppNavigator.applicationNavigation = ApplicationNavigatorItem(
        id: newUUID

Now to the SwiftUI half.

In my views, I’ve shared appDelegate as an setting object with my view, since I wish to hear for adjustments to

I’ve set an onChanged modifier to my primary view:

.onChange(of: { _ in
    DispatchQueue.primary.async {

And this solely works if the consumer is tapping the push notification when the app is within the background.

Nonetheless, if a consumer receives the push notification whereas they’re contained in the app, and it’s energetic, nothing occurs after they faucet it.

Oddly sufficient, after tapping a notification whereas contained in the app, (the place nothing occurs) if I swipe the app up till it is scenePhase is .inactive || .background, and re-open the app, the code inside .onChange(..) { _ in // code } lastly runs.

I’ve even put a Textual content(!) on the principle view to see if has modified, however it would not if I am nonetheless contained in the app.

I am making an attempt to ship the consumer to a selected view in the event that they faucet on a notification, and generally they could be contained in the app. Can anybody assist me see the place I’ve made a mistake?



Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments