How to reliably detect if an external keyboard is connected on iOS 9?

Sarah Elan

Previous to iOS 9, the most reliable method of determining whether an external keyboard is connected was to listen for UIKeyboardWillShowNotification and make a text field the first responder, as discussed in this question. The notification would fire when using the virtual keyboard, but would not fire when using an external keyboard.

However this behavior has now changed with iOS 9. UIKeyboardWillShowNotification also fires when an external keyboard is connected, since the new keyboard toolbar is now shown.

It is still possible to detect the keyboard height and make a judgement whether it is the smaller toolbar or the larger virtual keyboard that is being shown. However this method is not reliable since the keyboard height has changed between the various beta and can't be counted on to stay the same over time.

Is there a more reliable method that can be used with iOS 9?

Sarah Elan

After going back to the original question, I've found a solution that works.

It seems that when the regular virtual keyboard is displayed the keyboard frame is within the dimensions of the screen. However when a physical keyboard is connected and the keyboard toolbar is displayed, the keyboard frame is located offscreen. We can check if the keyboard frame is offscreen to determine if the keyboard toolbar is showing.

Objective-C

- (void) keyboardWillShow:(NSNotification *)notification {
    NSDictionary* userInfo = [notification userInfo];
    CGRect keyboardFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect keyboard = [self.view convertRect:keyboardFrame fromView:self.view.window];
    CGFloat height = self.view.frame.size.height;

    if ((keyboard.origin.y + keyboard.size.height) > height) {
        self.hasKeyboard = YES;
    }
}

Swift

@objc func keyboardWillShow(_ notification: NSNotification) {
    guard let userInfo = notification.userInfo else {return}
    let keyboardScreenEndFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
    let keyboard = self.view.convert(keyboardScreenEndFrame, from: self.view.window)
    let height = self.view.frame.size.height
    if (keyboard.origin.y + keyboard.size.height) > height {
        self.hasKeyboard = true
    }
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to reliably get keyboard height in iOS Swift?

分類Dev

How to detect if ios8 custom keyboard extension is running in not iphone 6 optimized app?

分類Dev

How to detect if Chromecast is already connected on Android Sender?

分類Dev

How detect the devices connected to wif network

分類Dev

Entity Framework: How to detect external changes to database

分類Dev

How can a JavaScript app detect if a Leap Motion device is connected

分類Dev

How to detect click event of connected Bluetooth peripheral device (Selfie stick)?

分類Dev

How to detect Swipe Gesture in iOS?

分類Dev

How to import external libraries in jshell java 9?

分類Dev

How to import external libraries in jshell java 9?

分類Dev

How to detect when keyboard is opened or closed in React Native

分類Dev

iOS - How to reference Keyboard Notification outside of method?

分類Dev

How to change UIWebView keyboard toolBar in iOS 7?

分類Dev

Swift (4.1) how to detect if me app was open from external source

分類Dev

Detect rings/circuits of connected voxels

分類Dev

How to detect if iOS Wifi hardware is ON swift 3

分類Dev

How to detect system timer going off in iOS

分類Dev

How to move data to Windows reliably?

分類Dev

How can I read input from the hosts keyboard when connected via SSH?

分類Dev

How do I make Mac OS X remember "Modifier Keys" remappings for my external keyboard?

分類Dev

How can we programmatically detect which iOS version is device running on?

分類Dev

Monitoring in Microservices : How to detect if a internal/external service goes down in Microservices Architecture

分類Dev

How to check if MQ is connected

分類Dev

How are these nested vectors connected?

分類Dev

How to view that Advertiser is connected

分類Dev

iOS Swift Interrupt Keyboard Events

分類Dev

iOS 8 custom keyboard on device

分類Dev

IOS 8 changing keyboard height

分類Dev

IOS Keyboard input from computer

Related 関連記事

  1. 1

    How to reliably get keyboard height in iOS Swift?

  2. 2

    How to detect if ios8 custom keyboard extension is running in not iphone 6 optimized app?

  3. 3

    How to detect if Chromecast is already connected on Android Sender?

  4. 4

    How detect the devices connected to wif network

  5. 5

    Entity Framework: How to detect external changes to database

  6. 6

    How can a JavaScript app detect if a Leap Motion device is connected

  7. 7

    How to detect click event of connected Bluetooth peripheral device (Selfie stick)?

  8. 8

    How to detect Swipe Gesture in iOS?

  9. 9

    How to import external libraries in jshell java 9?

  10. 10

    How to import external libraries in jshell java 9?

  11. 11

    How to detect when keyboard is opened or closed in React Native

  12. 12

    iOS - How to reference Keyboard Notification outside of method?

  13. 13

    How to change UIWebView keyboard toolBar in iOS 7?

  14. 14

    Swift (4.1) how to detect if me app was open from external source

  15. 15

    Detect rings/circuits of connected voxels

  16. 16

    How to detect if iOS Wifi hardware is ON swift 3

  17. 17

    How to detect system timer going off in iOS

  18. 18

    How to move data to Windows reliably?

  19. 19

    How can I read input from the hosts keyboard when connected via SSH?

  20. 20

    How do I make Mac OS X remember "Modifier Keys" remappings for my external keyboard?

  21. 21

    How can we programmatically detect which iOS version is device running on?

  22. 22

    Monitoring in Microservices : How to detect if a internal/external service goes down in Microservices Architecture

  23. 23

    How to check if MQ is connected

  24. 24

    How are these nested vectors connected?

  25. 25

    How to view that Advertiser is connected

  26. 26

    iOS Swift Interrupt Keyboard Events

  27. 27

    iOS 8 custom keyboard on device

  28. 28

    IOS 8 changing keyboard height

  29. 29

    IOS Keyboard input from computer

ホットタグ

アーカイブ