有办法做到这一点吗?我注册我的对象UIPasteboardChangedNotification
在启动时,但是当将其发送到后台并打开(例如)Safari 并复制一些文本时,我的处理程序永远不会被调用。
(我现在只使用模拟器)。
我都用过:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(pasteboardNotificationReceived:)
name:UIPasteboardChangedNotification
object:[UIPasteboard generalPasteboard]];
and:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(pasteboardNotificationReceived:)
name:UIPasteboardChangedNotification
object:nil ];
注册我的处理程序。
我有同样的问题。根据UIPasteboard类参考 http://developer.apple.com/library/ios/#documentation/uikit/reference/UIPasteboard_Class/Reference.html的文档changeCount
财产(重点是我的):
每当粘贴板的内容发生更改时(具体来说,当添加、修改或删除粘贴板项目时),UIPasteboard 都会递增此属性的值。在增加更改计数后,UIPasteboard 发布名为 UIPasteboardChangedNotification(用于添加和修改)和 UIPasteboardRemovedNotification(用于删除)的通知。 ...该类还更新更改计数当应用程序重新激活并且另一个应用程序更改了粘贴板内容时。当用户重新启动设备时,更改计数将重置为零。
我读到这意味着我的申请将收到UIPasteboardChangedNotification
我的应用程序重新激活后会收到通知。但仔细阅读就会发现,这只是changeCount
重新激活应用程序时会更新。
我通过跟踪粘贴板来处理这个问题changeCount
在我的应用程序委托中,并在找到时发布预期的通知changeCount
当应用程序在后台时已更改。
在应用程序委托的界面中:
NSUInteger pasteboardChangeCount_;
在应用程序委托的实现中:
- (BOOL)application:(UIApplication*)application
didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(pasteboardChangedNotification:)
name:UIPasteboardChangedNotification
object:[UIPasteboard generalPasteboard]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(pasteboardChangedNotification:)
name:UIPasteboardRemovedNotification
object:[UIPasteboard generalPasteboard]];
...
}
- (void)pasteboardChangedNotification:(NSNotification*)notification {
pasteboardChangeCount_ = [UIPasteboard generalPasteboard].changeCount;
}
- (void)applicationDidBecomeActive:(UIApplication*)application {
if (pasteboardChangeCount_ != [UIPasteboard generalPasteboard].changeCount) {
[[NSNotificationCenter defaultCenter]
postNotificationName:UIPasteboardChangedNotification
object:[UIPasteboard generalPasteboard]];
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)