Receiving UIPasteboard (generalPasteboard) notification while in the background

前端 未结 1 1056
旧巷少年郎
旧巷少年郎 2021-02-07 19:24

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)

相关标签:
1条回答
  • 2021-02-07 19:52

    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]];
      }
    }
    
    0 讨论(0)
提交回复
热议问题