質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

iPhone

iPhoneとは、アップル社が開発・販売しているスマートフォンです。 同社のデジタルオーディオプレーヤーiPodの機能、電話機能、インターネットやメールなどのWeb通信機能の3つをドッキングした機器です。

Q&A

解決済

1回答

797閲覧

addObserverでaSelectorとして登録したメソッドが発火しない

akadashi

総合スコア19

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

iPhone

iPhoneとは、アップル社が開発・販売しているスマートフォンです。 同社のデジタルオーディオプレーヤーiPodの機能、電話機能、インターネットやメールなどのWeb通信機能の3つをドッキングした機器です。

0グッド

0クリップ

投稿2020/12/31 10:45

編集2021/01/01 16:00

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)

初歩的な質問で申し訳ありませんが、よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hoshi-takanori

2021/01/04 14:51

普通の UIViewController じゃなくてカスタムキーボードなんですね。カスタムキーボードは作ったことがありませんが、自分自身が表示されるタイミングなら viewWillAppear で撮れるのでは。逆に、自分がアクティブじゃない時に他のキーボードも含めて表示されるタイミングを知りたいという話なら、もしかするとセキュリティ上の理由で禁止しているのかもしれません。
akadashi

2021/01/05 17:09

ありがとうございます! 自分自身が取れればよいだけなので、viewWillAppearを使おうと思います。 この回答でベストアンサーとさせていただきたいので、解決方法として投稿していただけませんか?
guest

回答1

0

ベストアンサー

普通の UIViewController じゃなくてカスタムキーボードなんですね。カスタムキーボードは作ったことがありませんが、自分自身が表示されるタイミングなら viewWillAppear で取れるのでは。逆に、自分がアクティブじゃない時に他のキーボードも含めて表示されるタイミングを知りたいという話なら、もしかするとセキュリティ上の理由で禁止しているのかもしれません。

投稿2021/01/05 17:21

hoshi-takanori

総合スコア7895

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問