Observer for "keyboardWillShowNotification" does not work

I added an observer for UIResponder.keyboardWillShowNotification as below (line 16), but it won't work.


Code Block Swift
import UIKit
class KeyboardViewController: UIInputViewController {
  @IBOutlet var nextKeyboardButton: UIButton!
   
  override func updateViewConstraints() {
    super.updateViewConstraints()
     
  }
   
  override func viewDidLoad() {
    super.viewDidLoad()
     
    print("########## Adding ##########")
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_ :)), name: UIResponder.keyboardWillShowNotification, object: nil)
    print("########## Added ##########")
     
    self.nextKeyboardButton = UIButton(type: .system)
     
    self.nextKeyboardButton.setTitle(NSLocalizedString("Next Keyboard", comment: "Title for 'Next Keyboard' button"), for: [])
    self.nextKeyboardButton.sizeToFit()
    self.nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = false
     
    self.nextKeyboardButton.addTarget(self, action: #selector(handleInputModeList(from:with:)), for: .allTouchEvents)
     
    self.view.addSubview(self.nextKeyboardButton)
     
    self.nextKeyboardButton.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
    self.nextKeyboardButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
  }
   
  override func viewWillLayoutSubviews() {
    self.nextKeyboardButton.isHidden = !self.needsInputModeSwitchKey
    super.viewWillLayoutSubviews()
  }
   
  override func textWillChange(_ textInput: UITextInput?) {
  }
   
  override func textDidChange(_ textInput: UITextInput?) {
     
    var textColor: UIColor
    let proxy = self.textDocumentProxy
    if proxy.keyboardAppearance == UIKeyboardAppearance.dark {
      textColor = UIColor.white
    } else {
      textColor = UIColor.black
    }
    self.nextKeyboardButton.setTitleColor(textColor, for: [])
  }
   
  @objc func keyboardWillShow(_ notification: Notification) {
    print("########## Keyboard will show ##########")
  }
}


Here are what I did:
  1. Create a new project > App

  2. Create a new target > Custom Keyboard Extention

  3. Activate

  4. Add the observer to line 16, and the function keyboardWillShow to line 56

  5. Run the simulator

  6. Add the custom keyboard, and open it

  7. Check logs

I was expecting all of the logs Adding (Line 15), Added (Line 17), and Keyboard will show (Line 57) were shown when opening the custom keyboard, but I found the last one was not shown.

Logs:
Code Block
2021-01-02 06:11:01.253544+0900 keyboard[19825:1761250] Failed to inherit CoreMedia permissions from 19823: (null)
########## Adding ##########
########## Added ##########
2021-01-02 06:11:01.300411+0900 keyboard[19825:1761156] [External] -[UIInputViewController needsInputModeSwitchKey] was called before a connection was established to the host application. This will produce an inaccurate result. Please make sure to call this after your primary view controller has been initialized.
2021-01-02 06:11:01.890932+0900 keyboard[19825:1761156] [External] -[UIInputViewController needsInputModeSwitchKey] was called before a connection was established to the host application. This will produce an inaccurate result. Please make sure to call this after your primary view controller has been initialized.
2021-01-02 06:11:01.924228+0900 keyboard[19825:1761156] [External] -[UIInputViewController needsInputModeSwitchKey] was called before a connection was established to the host application. This will produce an inaccurate result. Please make sure to call this after your primary view controller has been initialized.
2021-01-02 06:11:01.935400+0900 keyboard[19825:1761156] [External] -[UIInputViewController needsInputModeSwitchKey] was called before a connection was established to the host application. This will produce an inaccurate result. Please make sure to call this after your primary view controller has been initialized.
2021-01-02 06:11:01.935681+0900 keyboard[19825:1761156] [External] -[UIInputViewController needsInputModeSwitchKey] was called before a connection was established to the host application. This will produce an inaccurate result. Please make sure to call this after your primary view controller has been initialized.
2021-01-02 06:11:01.943204+0900 keyboard[19825:1761156] [External] -[UIInputViewController needsInputModeSwitchKey] was called before a connection was established to the host application. This will produce an inaccurate result. Please make sure to call this after your primary view controller has been initialized.


I would like to know if this issue can be reproduced in your side, and how to resolve it. Thank you


Xcode: 12.3
Swift: 5.3.2
Simulator: iPod touch (7th generation)


6. Add the custom keyboard, and open it

How do you open it ?

AFAIK, keyboardWillShow is called only when opened from a textField (when it becomes first responder)
https://stackoverflow.com/questions/64950310/ios-custom-keyboard-extension-not-posting-uikeyboardwillshow-and-uikeyboardwillh



How do you open it ?

Steps to add the keyboard:
Setting > General > Keyboard > Keyboards > Add New Keyboard…

Steps to open the keyboard:
Reminders > New Reminder > Focus on any text field such as Title and Notes > Switch keyboards > The custom keyboard is shown now


Did it work in your side?

Reminders > New Reminder > Focus on any text field such as Title and Notes > Switch keyboards > The custom keyboard is shown now

So you mean it opens but keyBoardWill show isn't called ?

So you mean it opens but keyBoardWill show isn't called ?

Yes. My custom keyboard opens, but keyBoardWillShow isn't called.
Accepted Answer

Yes. My custom keyboard opens, but keyBoardWillShow isn't called.

I have never heard that UIResponder.keyboardWillShowNotification can be detected in a keyboard extension.
It will be notified to the apps using keyboard, but not sure to a keyboard extension.

Have you ever found some documentation about it?

Have you ever found some documentation about it?

I couldn't find it. (I thought some web pages said keyboardWillShowNotification could be used in a keyboard extension, but it could be my misunderstanding.)

After trying different ways, I found viewWillAppear might be what I wanted, so I'll use it instead.
Thank you!
Observer for "keyboardWillShowNotification" does not work
 
 
Q