Skip to content

Instantly share code, notes, and snippets.

@vpaharia
Created November 3, 2016 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vpaharia/f9f79e534d0e75f98bf9a1b9faefee17 to your computer and use it in GitHub Desktop.
Save vpaharia/f9f79e534d0e75f98bf9a1b9faefee17 to your computer and use it in GitHub Desktop.
Determine current keyboard type in iOS 10 and iOS 9
//
// UISearchBar+DetermineKeyboardType.m
//
// message [self.searchBar determineKeyboardType] to retreive current keyboard type
//
// Problem is explained here: http://tech.ustwo.com/2014/05/29/ios-uikeyboardtype-issue/
// Most of the code is copied from https://github.com/martinstolz/UIKeyboardType-Fix-Demo
// few modification made to support iOS 10 && 9
//
#import "UISearchBar+DetermineKeyboardType.h"
#import <objc/message.h>
@implementation UISearchBar (DetermineKeyboardType)
- (UIKeyboardType)determineKeyboardType
{
UIWindow *keyboardWindow = [self keyboardWindow];
NSString *componentName = nil;
if(keyboardWindow)
[self getComponentName:keyboardWindow.subviews componentName:&componentName];
UIKeyboardType keyboardType = [self mapComponentNameToKeyboardType:componentName];
return keyboardType;
}
- (UIWindow *)keyboardWindow
{
UIWindow *keyboardWindow = nil;
NSArray *windows = [UIApplication sharedApplication].windows;
for (UIWindow *window in windows)
{
if ([window.class isEqual:NSClassFromString(@"UIRemoteKeyboardWindow")])
{
keyboardWindow = window;
break;
}
}
return keyboardWindow;
}
- (UIKeyboardType)mapComponentNameToKeyboardType:(NSString *)componentName
{
UIKeyboardType keyboardType = 0;
if (componentName) {
NSNumber *value = [[self mapDictionary] valueForKey:componentName];
if (value)
{
keyboardType = [value intValue];
}
}
return keyboardType;
}
- (NSDictionary *)mapDictionary
{
return @{
@"Capital-Letters": @(UIKeyboardTypeASCIICapable),
@"Small-Letters": @(UIKeyboardTypeASCIICapable),
@"First-Alternate": @(UIKeyboardTypeASCIICapable),
@"Numbers-And-Punctuation": @(UIKeyboardTypeNumbersAndPunctuation),
@"Numbers-And-Punctuation-Alternate": @(UIKeyboardTypeNumbersAndPunctuation)
};
}
- (void)getComponentName:(NSArray *)array componentName:(NSString **)componentName
{
for(int i = 0; i < array.count; i++)
{
if([array[i] isKindOfClass:[UIView class]])
{
UIView *view = (UIView *)array[i];
if ([NSStringFromClass(view.class) isEqualToString:@"UIKBKeyplaneView"])
{
SEL keyplaneSelector = NSSelectorFromString(@"keyplane");
if ([view respondsToSelector:keyplaneSelector])
{
NSObject *keyplane = ((NSString *(*)(id, SEL))objc_msgSend)(view, keyplaneSelector);
if (keyplane.class == NSClassFromString(@"UIKBTree"))
{
SEL componentNameSelector = NSSelectorFromString(@"componentName");
NSString *name = ((NSString *(*)(id, SEL))objc_msgSend)(keyplane, componentNameSelector);
*componentName = name;
return;
}
}
}
else
{
[self getComponentName:[view subviews] componentName:componentName];
}
}
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment