1

How can I update the badge on a tabview icon whenever a change to UIApplication.shared.applicationIconBadgeNumber occurs?

For instance, on a landing page where the number of unread messages are displayed:

struct LandingView: View {
  @State private var selectedTab: SelectedTab = .home
  
  var body: some View {
      MessagesView()
      .tabItem {
        Label("Messages", systemImage: "envelope")
      }
      .tag(SelectedTab.requests)
      .badge(UIApplication.shared.applicationIconBadgeNumber)       // <- How to have changes to this value recompute the view?
      
      HomeView()
      .tabItem {
        Label("Home", systemImage: "house")
      }
      .tag(SelectedTab.home)
    }
  }
}
cbusch
  • 265
  • 2
  • 11
  • Aren't you setting `UIApplication.shared.applicationIconBadgeNumber` by yourself from somewhere inside your app? Then just use this value to show the badge on your tab. – ChrisR Feb 07 '22 at 16:58
  • @ChrisR I set the iOS badge count by sending it in the payload of a push notification from my server. How can I listen to UIApplication.shared.applicationIconBadgeNumber for changes? – cbusch Feb 08 '22 at 02:43
  • does this help? https://stackoverflow.com/questions/64032039/swiftui-application-lifecycle – ChrisR Feb 08 '22 at 07:01

0 Answers0