I'm writing a custom keyboard for iOS and I'd like to detect when the user copies some text. I've read that you can use the NSNotificationCenter
along with UIPasteboardChangedNotification
in order to do this.
However, it seems my selector isn't getting fired when the user copies text. When I put a breakpoint on the addObserver
line it appears to get skipped over although breakpoints immediately before and after it are hit. Here is the code I am 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 determine what I'm missing?
Edit:
I noticed that the notification fires if I programmatically update the pasteboard after registering the notification, but I still can't figure out why it's not being hit if the user uses the context menu "copy".