3

My view controller registers to keyboard notifications (keyboardWillShow, keyboardWillHide).

I launch my app. It is showing the viewcontroller that is registered to keyboard notifications. The keyboard is not visible.

I switch to the sms app and start writing text. While I'm writing, my app gets a notification. The notification is displayed as a banner on the top of the screen.

When I click the banner, my app is opened and immediately gets a keyboard notification.

As far as I can tell, this keyboard notification is related to the keyboard of the SMS.

How do I identify if the keyboard event came from my app or not?

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
daramasala
  • 3,040
  • 2
  • 26
  • 33
  • 1
    One question though, why aren't you using textfield/textview delegates for knowing about keyboard notifications? They will be exclusive to your in-app editing events. – NSNoob Nov 12 '15 at 09:08
  • 1
    See my answer here http://stackoverflow.com/a/40031687/2774520 – Oleksii Nezhyborets Oct 13 '16 at 21:58
  • Does this answer your question? [keyboardWillShow gets called for other app's keyboards](https://stackoverflow.com/questions/34409566/keyboardwillshow-gets-called-for-other-apps-keyboards) – João Souza Dec 29 '19 at 15:41

1 Answers1

0

you can remove listening to observers (keyboard notifications) in viewWillDisappear and can start listening to observer again in viewWillAppear, this might solve the problem

- (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];
}
Usama
  • 1,945
  • 1
  • 13
  • 20