在后台接收UIPasteboard(generalPasteboard)通知 [英] Receiving UIPasteboard (generalPasteboard) notification while in the background

查看:1200
本文介绍了在后台接收UIPasteboard(generalPasteboard)通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有办法吗?我在启动时注册我的对象 UIPasteboardChangedNotification ,但是当它发送到背景和打开(例如)Safari和复制一些文本,我的处理程序从来没有被调用。
(我现在只使用模拟器)。

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).

我使用了两个:

[[NSNotificationCenter defaultCenter] addObserver:self 
    selector:@selector(pasteboardNotificationReceived:) 
    name:UIPasteboardChangedNotification 
    object:[UIPasteboard generalPasteboard]];

和:

[[NSNotificationCenter defaultCenter] addObserver:self 
    selector:@selector(pasteboardNotificationReceived:) 
    name:UIPasteboardChangedNotification 
    object:nil ];

注册我的处理程序。

推荐答案

我有同样的问题。根据 UIPasteboard类参考文档, changeCount 属性(重点是我的):

I had the same problem. According to the UIPasteboard Class Reference documentation for the changeCount property (emphasis is mine):


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

Whenever the contents of a pasteboard changes—specifically, when pasteboard items are added, modified, or removed—UIPasteboard increments the value of this property. After it increments the change count, UIPasteboard posts the notifications named UIPasteboardChangedNotification (for additions and modifications) and UIPasteboardRemovedNotification (for removals). ... The class also updates the change count when an application reactivates and another application has changed the pasteboard contents. When users restart a device, the change count is reset to zero.

我读过这个文件,意味着当我的应用程序重新激活后,我的应用程序会收到 UIPasteboardChangedNotification 通知。然而,仔细的阅读显示,只有在重新激活应用程序时才更新 changeCount

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 the changeCount that is updated when the app is reactivated.

我通过在我的应用程序委托中跟踪粘贴板的 changeCount 来处理这个问题,当我发现 changeCount

I dealt with this by tracking the pasteboard's changeCount in my app delegate and posting the expected notification when I find the changeCount has been changed while the app was in the background.

在应用程式委托介面中:

In the app delegate's interface:

NSUInteger pasteboardChangeCount_;

在应用程式委托的执行中:

And in the app delegate's implementation:

- (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]];
  }
}

这篇关于在后台接收UIPasteboard(generalPasteboard)通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆