最近在做一個(gè)富文本編輯器的功能,里面牽扯到一些原生洼冻、第三方軟鍵盤的判斷處理邏輯.我是這樣做的:
? ? 在
? ? ?func keyboardWillShow(notification:NSNotification){
????? ??????//判斷是原生輸入法 還是第三方輸入法
? ? ????? ? let key = UIKeyboardInputModeController.sharedInputModeController() as! UIKeyboardInputModeController
? ? ? ? ? ? let currentInputMode = key.currentInputMode()
? ? ? ? ????let extensionInputModes = key.extensionInputModes()as!NSArray
????? ? ? ? if extensionInputModes .contains(currentInputMode) {
????????? ? ? ? ? ? ????//第三方鍵盤
? ? ????????????? ? }else{
? ? ? ? ? ? ? ? ? ?????//原生鍵盤
? ? ? ? ? ? ? ? ? ?}
????}
? ? 這個(gè)方法也會(huì)隨著你點(diǎn)擊小地球,即切換輸入法(并不是拼音、五筆眼坏、拼音等)的響應(yīng)而一直響應(yīng)的!
? ? 以上用到的UIKeyboardInputModeController樟凄、UIKeyboardInputMode這兩個(gè)類,樓主也是在網(wǎng)上淘的,復(fù)制如下:(OC寫的)
#ifndef TestDeterminKeyboard_UIKeyboardInputMode_h
#define TestDeterminKeyboard_UIKeyboardInputMode_h
#import
@class NSString, NSArray;
@interfaceUIKeyboardInputMode :UITextInputMode{
}
@property (nonatomic,copy) NSString * identifier;
@property (nonatomic,readonly) NSString * primaryLanguage;
@property(nonatomic,copy)NSString* softwareLayout;
@property(nonatomic,copy)NSString* hardwareLayout;
@property(nonatomic,readonly)NSArray* normalizedIdentifierLevels;
//+(id)keyboardInputModeWithIdentifier:(id)arg1 ;
//+(id)canonicalLanguageIdentifierFromIdentifier:(id)arg1 ;
//+(id)softwareLayoutFromIdentifier:(id)arg1 ;
//+(id)hardwareLayoutFromIdentifier:(id)arg1 ;
//-(void)dealloc;
//-(id)initWithCoder:(id)arg1 ;
//-(void)encodeWithCoder:(id)arg1 ;
//-(BOOL)isEqual:(id)arg1 ;
//-(id)hardwareLayout;
//-(id)identifier;
//-(id)initWithIdentifier:(id)arg1 ;
//-(void)setIdentifier:(id)arg1 ;
//-(id)primaryLanguage;
//-(void)setPrimaryLanguage:(id)arg1 ;
//-(void)setSoftwareLayout:(id)arg1 ;
//-(void)setHardwareLayout:(id)arg1 ;
//-(id)normalizedIdentifierLevels;
//-(id)softwareLayout;
@end
@interfaceUIKeyboardInputModeController :NSObject{
? ? UIKeyboardInputMode* _currentInputMode;
? ? NSArray* _inputModesWithoutHardwareSupport;
? ? NSString* currentLocale;
? ? NSString* currentLanguage;
? ? NSArray* keyboardInputModes;
? ? NSArray* enabledInputModes;
? ? NSArray* normalizedInputModes;
? ? NSArray* defaultKeyboardInputModes;
? ? NSArray* defaultRawInputModes;
? ? NSArray* defaultInputModes;
? ? NSArray* defaultNormalizedInputModes;
? ? NSString* _inputModeContextIdentifier;
}
+ (id)sharedInputModeController;
- (id)_MCFilteredExtensionIdentifiers;
- (void)_clearAllExtensionIfNeeded;
- (void)_setCurrentInputMode:(id)arg1 force:(BOOL)arg2;
- (id)_systemInputModePassingTest:(id)arg1;
- (id)activeInputModeIdentifiers;
- (id)activeInputModes;
- (id)allowedExtensions;
- (BOOL)containsDictationSupportedInputMode;
- (id)currentInputMode;
- (id)currentInputModeInPreference;
- (BOOL)currentLocaleRequiresExtendedSetup;
- (id)currentPublicInputMode;
- (id)currentSystemInputMode;
- (id)currentUsedInputMode;
- (void)dealloc;
- (id)defaultEnabledInputModesForCurrentLocale:(BOOL)arg1;
- (id)defaultInputModes;
- (id)defaultKeyboardInputModes;
- (id)defaultNormalizedInputModes;
- (id)defaultRawInputModes;
- (id)delegate;
- (BOOL)deviceStateIsLocked;
- (void)didEnterBackground:(id)arg1;
- (id)enabledInputModeIdentifiers:(BOOL)arg1;
- (id)enabledInputModeIdentifiers;
- (id)enabledInputModeLanguages;
- (id)enabledInputModes;
- (id)extensionInputModes;
- (id)hardwareInputMode;
- (BOOL)identifierIsValidSystemInputMode:(id)arg1;
- (id)identifiersFromInputModes:(id)arg1;
- (id)init;
- (id)inputModeContextIdentifier;
- (id)inputModeWithIdentifier:(id)arg1;
- (id)inputModesWithoutHardwareSupport;
- (id)keyboardInputModeIdentifiers;
- (id)keyboardInputModes;
- (id)lastUsedInputMode;
- (id)nextInputModeFromList:(id)arg1 withFilter:(unsignedint)arg2 withTraits:(id)arg3;
- (id)nextInputModeInPreferenceListForTraits:(id)arg1;
- (id)nextInputModeToUse;
- (id)nextInputModeToUseForTraits:(id)arg1;
- (id)normalizedEnabledInputModeIdentifiers;
- (id)normalizedInputModes;
- (void)performWithForcedExtensionInputModes:(id)arg1;
- (void)performWithoutExtensionInputModes:(id)arg1;
- (id)preferredLanguages;
- (void)setCurrentInputMode:(id)arg1;
- (void)setCurrentInputModeInPreference:(id)arg1;
- (void)setCurrentUsedInputMode:(id)arg1;
- (void)setDefaultInputModes:(id)arg1;
- (void)setDefaultKeyboardInputModes:(id)arg1;
- (void)setDefaultNormalizedInputModes:(id)arg1;
- (void)setDefaultRawInputModes:(id)arg1;
- (void)setDelegate:(id)arg1;
- (void)setEnabledInputModes:(id)arg1;
- (void)setInputModeContextIdentifier:(id)arg1;
- (void)setKeyboardInputModeIdentifiers:(id)arg1;
- (void)setKeyboardInputModes:(id)arg1;
- (void)setLastUsedInputMode:(id)arg1;
- (void)setNextInputModeToUse:(id)arg1;
- (void)setNormalizedInputModes:(id)arg1;
- (void)startConnectionForFileAtURL:(id)arg1 forInputModeIdentifier:(id)arg2;
- (void)startDictationConnectionForFileAtURL:(id)arg1 forInputModeIdentifier:(id)arg2;
- (id)suggestedInputModesForCurrentLocale:(BOOL)arg1 fallbackToDefaultInputModes:(BOOL)arg2;
- (id)suggestedInputModesForCurrentLocale;
- (id)suggestedInputModesForPreferredLanguages;
- (id)supportedInputModeIdentifiers;
- (void)switchToCurrentSystemInputMode;
- (void)updateCurrentAndNextInputModes;
- (void)updateCurrentInputMode:(id)arg1;
- (void)updateLastUsedInputMode:(id)arg1;
- (BOOL)verifyKeyboardExtensionsWithApp;
@end
#endif
????自己建一個(gè)這樣的類,粘貼以上代碼.在使用處倒入頭文件,即可直接使用!
? ? 不過在這里值得提一句的是:UIKeyboardInputMode這個(gè)屬于私有Api,上架時(shí)審核會(huì)被拒絕!
? ? 最后用的方法是:
? ? NSString *currentKeyboardName = [[[[UITextInputMode activeInputModes] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"isDisplayed = YES"]] lastObject] valueForKey:@"extendedDisplayName"];
? ? if([currentKeyboardNameisEqualToString:@"簡體拼音"] || [currentKeyboardNameisEqualToString:@"表情符號(hào)"] || [currentKeyboardName isEqualToString:@"English (US)"]) {
? ? ? ? //系統(tǒng)自帶鍵盤
? ? ? ? returnYES;
? ? }else{
? ? ? ? //第三方鍵盤
? ? ? ? returnNO;
? ? }
? ? 如有什么不會(huì)的地方,請(qǐng)各位大神指出!??????