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

前端 未结 9 726
隐瞒了意图╮
隐瞒了意图╮ 2020-12-02 13:05

Previous to iOS 9, the most reliable method of determining whether an external keyboard is connected was to listen for UIKeyboardWillShowNotification and make a

9条回答
  •  悲哀的现实
    2020-12-02 13:54

    Private API solution: (have to grab the private header file - use RuntimeViewer).

    Works nicely for enterprise apps, where you don't have AppStore restrictions.

    #import "UIKit/UIKeyboardImpl.h"
    
    + (BOOL)isHardwareKeyboardMode
    {
       UIKeyboardImpl *kbi = [UIKeyboardImpl sharedInstance];
       BOOL externalKeyboard = kbi.inHardwareKeyboardMode;
       NSLog(@"Using external keyboard? %@", externalKeyboard?@"YES":@"NO");
       return externalKeyboard;
    }
    

提交回复
热议问题