2015-11-12 23 views
2

我的视图控制器注册到键盘通知(keyboardWillShow,keyboardWillHide)。iOS 9.0 - 获取键盘将显示/隐藏来自其他应用的通知

我启动我的应用程序。它显示了注册到键盘通知的视图控制器。键盘不可见。

我切换到短信应用程序,并开始写文本。在写作时,我的应用会收到通知。通知显示为屏幕顶部的横幅。

当我点击横幅时,我的应用程序被打开并立即获得键盘通知。

据我所知,此键盘通知与SMS的键盘有关。

如何识别键盘事件是否来自我的应用程序?

+0

一个问题,代表了解键盘通知?它们将仅限于您的应用内编辑活动。 – NSNoob

+0

在此处查看我的答案http://stackoverflow.com/a/40031687/2774520 –

回答

0

你可以删除viewWillDisappear听观察员(键盘通知),并可开始viewWillAppear再次听取观察员,这可能你为什么不使用文本框/ TextView的解决问题,但

- (void)registerForKeyboardNotifications 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWasShown:) 
               name:UIKeyboardWillShowNotification object:nil]; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWillBeHidden:) 
               name:UIKeyboardWillHideNotification object:nil]; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(applicationWillEnterBackground:) 
               name:UIApplicationWillResignActiveNotification 
               object:nil]; 
} 

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillDisappear:animated]; 
    [self registerForKeyboardNotifications]; 
} 

- (void)deregisterForKeyboardNotifications { 
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; 
    [center removeObserver:self name:UIKeyboardWillShowNotification object:nil]; 
    [center removeObserver:self name:UIKeyboardWillHideNotification object:nil]; 
    [center removeObserver:self name:UIApplicationWillResignActiveNotification object:nil]; 
} 

- (void)viewWillDisappear:(BOOL)animated { 
    [super viewWillDisappear:animated]; 
    [self deregisterForKeyboardNotifications]; 
}