NSNotificationCenter PasteboardChangedNotification未触发 [英] NSNotificationCenter PasteboardChangedNotification Not Firing
问题描述
我正在为iOS编写自定义键盘,我想检测用户何时复制一些文本。我已经读过你可以使用 NSNotificationCenter
以及 UIPasteboardChangedNotification
来执行此操作。
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.
但是,当用户复制文本时,我的选择器似乎没有被触发。当我在 addObserver
行上设置断点时,虽然它被击中之前和之后的断点,但它似乎被跳过了。这是我正在使用的代码:
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?
修改:
我注意到如果我以编程方式更新了通知,则会触发通知注册通知后粘贴板,但如果用户使用上下文菜单复制,我仍然无法弄清楚它为什么没有被击中。
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".
推荐答案
我的不是一个完美但功够强大的解决方案。我已经在键盘上使用它了。
My not a perfect but work enough solution. I already use it in 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
这篇关于NSNotificationCenter PasteboardChangedNotification未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!