Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSNotificationCenter粘贴板更改通知未触发_Ios_Swift_Keyboard - Fatal编程技术网

Ios NSNotificationCenter粘贴板更改通知未触发

Ios NSNotificationCenter粘贴板更改通知未触发,ios,swift,keyboard,Ios,Swift,Keyboard,我正在为iOS编写一个自定义键盘,我想检测用户何时复制一些文本。我已经了解到,您可以使用NSNotificationCenter和UIPasteboardChangedNotification来执行此操作 然而,当用户复制文本时,我的选择器似乎没有被触发。当我在addObserver行上放置一个断点时,它似乎被跳过了,尽管在命中之前和之后都有断点。以下是我正在使用的代码: override init(nibName nibNameOrNil: String?, bundle nibBundleO

我正在为iOS编写一个自定义键盘,我想检测用户何时复制一些文本。我已经了解到,您可以使用
NSNotificationCenter
UIPasteboardChangedNotification
来执行此操作

然而,当用户复制文本时,我的选择器似乎没有被触发。当我在
addObserver
行上放置一个断点时,它似乎被跳过了,尽管在命中之前和之后都有断点。以下是我正在使用的代码:

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
}
谁能确定我遗漏了什么

编辑:


我注意到,如果我在注册通知后以编程方式更新粘贴板,则会触发通知,但我仍然不明白,如果用户使用上下文菜单“复制”,为什么不会触发通知。

您不能使用NSNotificationCenter,因为键盘扩展是另一个过程

Cocoa包括两种类型的通知中心: NSNotificationCenter类在单个 过程NSDistributedNotificationCenter类管理 在一台计算机上跨多个进程发送通知

尝试此解决方案:它使用CFNotifications。不要忘记启用应用程序组以访问共享容器


这里有更多代码:&

我的不是一个完美的解决方案,但已经足够有效了。我已经在键盘上用过了

@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

你知道怎么做吗?ThanksI最终不得不使用nstimer轮询粘贴板的更改。我觉得这不是最好的方法。谢谢你的回复!是的,我现在就是这样做的,我不喜欢。我不知道为什么UIPasteboardChangedNotification不起作用。这一定是一个bug或者它被阻止了。亲爱的@maverson如果你有任何“更好的解决方案”,请分享。如果你不喜欢我的解决方案,那就不要点击减号,因为它有效,而且你没有提供更好的解决方案。我有点困惑,我把你的解决方案标记为已接受,因为我最终使用了类似的解决方案。我当然没有投反对票,你可以查看我的投票记录。谢谢你的帮助!