NSNotificationCenter PasteboardChangedNotification Not Firing
I am writing a custom keyboard for iOS and I would like to detect when the user copies some text. I read that it can be used in NSNotificationCenter
conjunction with UIPasteboardChangedNotification
.
However, it looks like my selector is not firing when the user copies the text. When I put a breakpoint on a line addObserver
, it is skipped, even though the breakpoints immediately and after it are hit. Here is the code I'm using:
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
// Register copy notifications
NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleCopy:", name: UIPasteboardChangedNotification, object: nil)
}
func handleCopy(sender: NSNotification) {
//todo: handle the copied text event
}
Can anyone identify what I am missing?
Edit:
I noticed that the notification fires if I programmatically update the filing cabinet after registering the notification, but I still can't figure out why it doesn't get hit if the user uses the "copy" context menu.
source to share
Mine is not perfect, but it works enough . I already use it on keyboard.
@interface MyPrettyClass : UIViewController
@end
@implementation MyPrettyClass
@property (strong, nonatomic) NSTimer *pasteboardCheckTimer;
@property (assign, nonatomic) NSUInteger pasteboardchangeCount;
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
_pasteboardchangeCount = [[UIPasteboard generalPasteboard] changeCount];
//Start monitoring the paste board
_pasteboardCheckTimer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(monitorBoard:)
userInfo:nil
repeats:YES];
}
- (void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
[self stopCheckingPasteboard];
}
#pragma mark - Background UIPasteboard periodical check
- (void) stopCheckingPasteboard{
[_pasteboardCheckTimer invalidate];
_pasteboardCheckTimer = nil;
}
- (void) monitorBoard:(NSTimer*)timer{
NSUInteger changeCount = [[UIPasteboard generalPasteboard]; changeCount];
if (changeCount != _pasteboardchangeCount) { // means pasteboard was changed
_pasteboardchangeCount = changeCount;
//Check what is on the paste board
if ([_pasteboard containsPasteboardTypes:pasteboardTypes()]){
NSString *newContent = [UIPasteboard generalPasteboard].string;
_pasteboardContent = newContent;
[self tryToDoSomethingWithTextContent:newContent];
}
}
}
- (void)tryToDoSomethingWithTextContent:(NSString)newContent{
NSLog(@"Content was changed to: %@",newContent);
}
@end
source to share
You cannot use NSNotificationCenter as keyboard expansion is a different process.
Cocoa includes two types of notification centers: The NSNotificationCenter class manages notifications in a single handle. The NSDistributedNotificationCenter class manages notifications across multiple processes on the same computer.
Try this solution: fooobar.com/questions/414739 / ... which uses CFNotifications. Don't forget to enable app groups to access the shared container.
More details here: https://github.com/cxa/AppExtensionCommunicator and https://github.com/mutualmobile/MMWormhole
source to share