2020-01-01 追記:
この現象が他の環境でも再現するのかどうかだけでも知りたいので、もし試された方がいらっしゃいましたら教えてください。よろしくお願いします。
前提・実現したいこと
iPhoneのソフトウェアキーボードが表示されようとしている瞬間に、指定したメソッドが発火されるようにしたい。
発生している問題・エラーメッセージ
NotificationCenter.default.addObserverでkeyboardWillShowというメソッドを登録しましたが、シミュレータでキーボードを表示しても発火されていないように見えます。
該当のソースコード
Swift
1import UIKit 2 3class KeyboardViewController: UIInputViewController { 4 5 @IBOutlet var nextKeyboardButton: UIButton! 6 7 override func updateViewConstraints() { 8 super.updateViewConstraints() 9 10 // Add custom view sizing constraints here 11 } 12 13 override func viewDidLoad() { 14 super.viewDidLoad() 15 16 print("keyboardWillShow登録") 17 NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil) 18 print("keyboardWillShow登録完了") 19 20 // Perform custom UI setup here 21 self.nextKeyboardButton = UIButton(type: .system) 22 23 self.nextKeyboardButton.setTitle(NSLocalizedString("Next Keyboard", comment: "Title for 'Next Keyboard' button"), for: []) 24 self.nextKeyboardButton.sizeToFit() 25 self.nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = false 26 27 self.nextKeyboardButton.addTarget(self, action: #selector(handleInputModeList(from:with:)), for: .allTouchEvents) 28 29 self.view.addSubview(self.nextKeyboardButton) 30 31 self.nextKeyboardButton.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true 32 self.nextKeyboardButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true 33 } 34 35 override func viewWillLayoutSubviews() { 36 self.nextKeyboardButton.isHidden = !self.needsInputModeSwitchKey 37 super.viewWillLayoutSubviews() 38 } 39 40 override func textWillChange(_ textInput: UITextInput?) { 41 // The app is about to change the document's contents. Perform any preparation here. 42 } 43 44 override func textDidChange(_ textInput: UITextInput?) { 45 // The app has just changed the document's contents, the document context has been updated. 46 47 var textColor: UIColor 48 let proxy = self.textDocumentProxy 49 if proxy.keyboardAppearance == UIKeyboardAppearance.dark { 50 textColor = UIColor.white 51 } else { 52 textColor = UIColor.black 53 } 54 self.nextKeyboardButton.setTitleColor(textColor, for: []) 55 } 56 57 @objc func keyboardWillShow(notification: NSNotification) { 58 print("keyboardWillShow発火") 59 } 60 61}
上記のコードは、Xcodeで新規Projectを作成(Appを選択)し、新規Targetを作成(Custom Keyboard Extentionを選択)し、Activateしたときのデフォルトのコードに対して以下2つのコードを追加したものです。
print("keyboardWillShow登録") NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil) print("keyboardWillShow登録完了")
@objc func keyboardWillShow(notification: NSNotification) { print("keyboardWillShow発火") }
実行時に出力されたログは以下のとおりです。シミュレータからキーボードを表示した際に「print("keyboardWillShow発火")」が表示されることを期待していますが、実際には出ていません。
2020-12-31 19:21:51.515965+0900 keyboard[11374:907995] Failed to inherit CoreMedia permissions from 11373: (null) 2020-12-31 19:21:51.559759+0900 keyboard[11374:907911] [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. 2020-12-31 19:21:51.587650+0900 keyboard[11374:907911] [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. keyboardWillShow登録 keyboardWillShow登録完了 2020-12-31 19:21:52.159890+0900 keyboard[11374:907911] [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. 2020-12-31 19:21:52.160607+0900 keyboard[11374:907911] [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. 2020-12-31 19:21:52.161765+0900 keyboard[11374:907911] [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.
補足情報(FW/ツールのバージョンなど)
Xcode: 12.3
Swift: 5.3.2
シミュレータ: iPod touch (7th generation)
初歩的な質問で申し訳ありませんが、よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー