2
3

More than 3 years have passed since last update.

[Swift] 現在開かれているキーボードを取得する

Last updated at Posted at 2021-02-04

ユーザが今どのキーボードを使っているか判断したい場合、次のように行います。

let currentKeyboardName = NSArray(array: UITextInputMode.activeInputModes)
                              .filtered(using: NSPredicate(format: "isDisplayed = YES"))
                              .first
                              .flatMap{($0 as? UITextInputMode)?.value(forKey: "displayName") as? String}
print(currentKeyboardName)  //例:Optional("日本語ローマ字")

以上をUIApplication.keyboardDidShowNotificationなどのタイミングで行うと開かれているキーボードを取得することができます。
キーボードアプリでユーザがキーボードを有効化したかどうかの判定に使えるほか、相性の悪い特定のサードパーティ製キーボードを禁止するなどの処理が必要な場合に使えるかもしれません。
なお、"displayName"ではなく"identifier"を用いるとキーボードの"identifier"を取得することができます。こちらの方がより正確に特定が可能です。

参考

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3