swift - 如何在 SwiftUI 中为 Watchkit 注册前台通知?

标签 swift swiftui watchkit watchos foregroundnotification

我想在 watch 应用进入前台时收到通知。如何在 SwiftUI 中注册“willEnterForegroundNotification”。以下是在 UIKit 中注册相同内容的代码。

Text("Hello, World!")
    .onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
        print("Moving back to the foreground!")
    }

最佳答案

在 watchOS 7 及更高版本上

            NotificationCenter.default.publisher(for: WKExtension.applicationWillEnterForegroundNotification)

关于swift - 如何在 SwiftUI 中为 Watchkit 注册前台通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60955722/

相关文章:

ios - 二进制拒绝 - 无效的 Watchkit 支持

swift - 格式为 HH :mm 的 CEST 时间

ios - Swift - 使用 CoreData 和 CollectionView 进行分页

ios - SwiftUI:如何增加可以触发按钮的区域?

swift - 当涉及协调器时,如何在 SwiftUI View 与另一个 View 之间进行通信?

ios - 无法将 1024px 应用商店图标添加到 watchOS 应用 Assets

Swift 2 - 当 AlertController 出现时如何切换到另一个 ViewController?

ios - 使用 midX 时,scrollView 图像不在中心?

ios - 如何禁用 SwiftUI 的默认行为?

ios - iTunes Store 操作失败错误 ITMS-90701 : "Missing entitlement?