There is no public API for this, but I found a solution for you, which requires very little "gray area API" (I define API as "gray area" if an API is not normally exposed, but can be hidden with little to no work).
iOS has the following class: UITextInputMode
This class gives you all the input methods the user can use. Using the following query will give you the currently used, only when the keyboard is open:
UITextInputMode* inputMode = [[[UITextInputMode activeInputModes] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"isDisplayed = YES"]] lastObject];
To get the display name of the extension (or regular Apple keyboard), use:
[inputMode valueForKey:@"displayName"]
or
[inputMode valueForKey:@"extendedDisplayName"]
This only works when the keyboard is visible. So you will have to monitor input mode change yourself using
[[NSNotificationCenter defaultCenter] addObserverForName:UITextInputCurrentInputModeDidChangeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note)
{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"%@", [[[[UITextInputMode activeInputModes] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"isDisplayed = YES"]] lastObject] valueForKey:@"extendedDisplayName"]);
});
}];
We actually need to delay obtaining the current input mode, as the notification is sent before the keyboard internal implementation has updated the system with new value. Obtaining it on the next runloop works well.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…