在后台接收UIPasteboard(generalPasteboard)通知

时间:2010-11-21 20:12:50

标签: cocoa-touch 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 ];

注册我的处理程序。

1 个答案:

答案 0 :(得分:11)

我遇到了同样的问题。根据{{​​1}}属性的UIPasteboard Class Reference文档(重点是我的):

  

每当粘贴板的内容发生更改时 - 特别是添加,修改或删除粘贴板项时 - UIPasteboard会增加此属性的值。在增加更改计数后,UIPasteboard会发布名为UIPasteboardChangedNotification(用于添加和修改)和UIPasteboardRemovedNotification(用于删除)的通知。 ...当应用程序重新激活并且另一个应用程序更改了粘贴板内容时,该类还会更新更改计数当用户重新启动设备时,更改计数将重置为零。

我读过这封信是为了表示我的应用会在重新启动应用后收到changeCount次通知。但仔细阅读后会发现,重新启动应用时只会更新UIPasteboardChangedNotification

我通过跟踪我的应用代表中的粘贴板changeCount来处理此问题,并在应用程序处于后台时发现changeCount已被更改时发布预期通知。

在app delegate的界面中:

changeCount

在app delegate的实现中:

NSUInteger pasteboardChangeCount_;
相关问题