I added an observer for UIResponder.keyboardWillShowNotification as below (line 16), but it won't work.
Here are what I did:
Logs:
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)
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:
Create a new project > App
Create a new target > Custom Keyboard Extention
Activate
Add the observer to line 16, and the function keyboardWillShow to line 56
Run the simulator
Add the custom keyboard, and open it
Check logs
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)