Thursday, January 26, 2023
HomeiOS Developmentios - onChange not detecting AppDelegate Update in SwiftUI

ios – onChange not detecting AppDelegate Update in SwiftUI


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

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

This is my ApplicationNavigator View Model:

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

Where ApplicationNavigator takes an ApplicationNavigatorItem:

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

And when a user taps on a remote notification, I update the AppNavigator in my AppDelegate with a new ApplicationNavigatorItem:

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

    self.AppNavigator.applicationNavigation = ApplicationNavigatorItem(
        id: newUUID

Now to the SwiftUI part.

In my views, I’ve shared appDelegate as an environment object with my view, since I want to listen for changes to

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

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

And this only works if the user is tapping the push notification when the app is in the background.

However, if a user receives the push notification while they’re inside the app, and it is active, nothing happens when they tap it.

Oddly enough, after tapping a notification while inside the app, (where nothing happens) if I swipe the app up until it’s scenePhase is .inactive || .background, and re-open the app, the code inside .onChange(..) { _ in // code } finally runs.

I’ve even put a Text(!) on the main view to see if has changed, but it doesn’t if I’m still inside the app.

I’m trying to send the user to a specific view if they tap on a notification, and sometimes they might be inside the app. Can anyone help me see where I’ve made a mistake?


Source link



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments