UIApplication.applicationIconBadgeNumber 已弃用

问题描述 投票:0回答:2
.onAppear {
  UIApplication.shared.applicationIconBadgeNumber = 0
}

它说 applicationIconBadgeNumber 已贬值,我必须使用

setBadge()
,我如何在这段代码中使用它?因为它这么说

applicationIconBadgeNumber 在 iOS 17.0 中已弃用:使用 -[UNUserNotificationCenter setBadgeCount:withCompletionHandler:] 代替。

现在我问我如何解决这个问题

swift swiftui notifications
2个回答
0
投票

试试这个:

.onAppear {
    UNUserNotificationCenter.current().setBadgeCount(0, withCompletionHandler: nil))
}

0
投票

由于弃用没有明确说明,您需要改为调用

UNUserNotificationCenter.setBadgeCount(_:withCompletionHandler:)
- docs

.onAppear {
  UNUserNotificationCenter.current().setBadgeCount(0)
}

如果你想处理可能的错误,也可以传递一个完成处理程序:

.onAppear {
  UNUserNotificationCenter.current().setBadgeCount(0) { error in
    guard let error else {
      // Badge count was successfully updated
      return
    }
    // Replace this with proper error handling
    print(error)
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.