使用UIKit在macOS上检测前台应用的重新签名。

huangapple go评论80阅读模式
英文:

Detect resigning frontmost app with UIKit on macOS

问题

我有一个 iPad 应用程序,通过 WebSocket 接收聊天消息。如果应用程序在前台运行时收到消息,消息会被标记为已读,因为用户很可能已经阅读了它。

在 macOS 上运行应用程序时,它始终处于前台且活动状态。只有在应用程序最小化时才会发送 willResignActiveNotificationdidEnterBackgroundNotification 通知。这意味着当前,即使在应用程序被其他应用程序覆盖时收到消息,也会被标记为已读,即使用户可能无法看到它。

如何判断应用程序是否不是最前面的应用程序?

我看到 AppKit 中有一个 frontmostApplication 的概念,但在 UIKit 中似乎没有相应的概念。

英文:

I have an iPad app that receives chat messages through a web socket. If the app is in the foreground when this happens, the message is marked as read since the user is likely to have read it.

When running the app on macOS it is always foreground and active. The willResignActiveNotification and didEnterBackgroundNotification notifications are only sent if the app is minimized. This means that at present a message received when the app is covered by another app, will be marked as read even if the use can't have seen it.

How can to tell if the app is not the frontmost app?

I see that AppKit has a concept of a frontmostApplication, but there doesn't seem to be any equivelent in UIKit.

答案1

得分: 2

在UIKit中,没有直接的API可以确定您的应用是否是macOS上的前置应用程序。前置应用程序的概念在UIKit的公共API中(无论是iOS还是macOS)都未公开。您可以考虑的一种方法是观察由AppKit提供的NSWorkspace类中的NSWorkspaceDidActivateApplicationNotification通知。尽管这是AppKit特定的,但您仍然可以在您的macOS应用程序中与UIKit一起使用它。

英文:

In UIKit, there is no direct API to determine if your app is the frontmost app on macOS. The concept of frontmost application is not exposed through public APIs in UIKit for iOS or macOS. One approach you can consider is to observe the NSWorkspaceDidActivateApplicationNotification notification from the NSWorkspace class provided by AppKit. Although it is AppKit-specific, you can still use it in your macOS app alongside UIKit.

huangapple
  • 本文由 发表于 2023年6月15日 06:56:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76478065.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定