Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receiving UIPasteboard (generalPasteboard) notification while in the background

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.

like image 936
NuBie Avatar asked Nov 21 '10 20:11

NuBie


1 Answers

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

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.

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.

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]];
  }
}
like image 136
Hilton Campbell Avatar answered Sep 24 '22 22:09

Hilton Campbell