在后台接收 UIPasteboard (generalPasteboard) 通知
有办法做到这一点吗?我在启动时为 UIPasteboardChangedNotification
注册了我的对象,但是当将其发送到后台并打开(例如)Safari 并复制一些文本时,我的处理程序永远不会被调用。 (我现在只使用模拟器)。
我使用了:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(pasteboardNotificationReceived:)
name:UIPasteboardChangedNotification
object:[UIPasteboard generalPasteboard]];
和:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(pasteboardNotificationReceived:)
name:UIPasteboardChangedNotification
object:nil ];
来注册我的处理程序。
In there a way to do this? I register my object for UIPasteboardChangedNotification
at launch time, but when sending it to the background and opening (for instance) Safari and copying some text, my handler never gets called.
(I'm using just the simulator for now).
I've used both:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(pasteboardNotificationReceived:)
name:UIPasteboardChangedNotification
object:[UIPasteboard generalPasteboard]];
and:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(pasteboardNotificationReceived:)
name:UIPasteboardChangedNotification
object:nil ];
to register my handler.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也有同样的问题。根据 UIPasteboard 类参考文档
changeCount
属性(重点是我的):我读到这意味着一旦我的应用程序重新激活,我的应用程序就会收到
UIPasteboardChangedNotification
通知。然而,仔细阅读后发现,重新激活应用程序时,只有changeCount
会更新。我通过在应用程序委托中跟踪粘贴板的
changeCount
来处理此问题,并在发现应用程序处于后台时changeCount
已更改时发布预期的通知。在应用程序委托的接口中:
在应用程序委托的实现中:
I had the same problem. According to the UIPasteboard Class Reference documentation for the
changeCount
property (emphasis is mine):I had read this to mean that my application would receive
UIPasteboardChangedNotification
notifications once my app was reactivated. A careful reading reveals, however, that it is only thechangeCount
that is updated when the app is reactivated.I dealt with this by tracking the pasteboard's
changeCount
in my app delegate and posting the expected notification when I find thechangeCount
has been changed while the app was in the background.In the app delegate's interface:
And in the app delegate's implementation: