iOS 判断是第三方软键盘(第三方输入法)还是原生软键盘(原生输入法)

最近在做一个富文本编辑器的功能,里面牵扯到一些原生、第三方软键盘的判断处理逻辑.我是这样做的:

    在

     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{

                       //原生键盘

                   }

    }

    这个方法也会随着你点击小地球,即切换输入法(并不是拼音、五笔、拼音等)的响应而一直响应的!

    以上用到的UIKeyboardInputModeController、UIKeyboardInputMode这两个类,楼主也是在网上淘的,复制如下:(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

    自己建一个这样的类,粘贴以上代码.在使用处倒入头文件,即可直接使用!

    不过在这里值得提一句的是:UIKeyboardInputMode这个属于私有Api,上架时审核会被拒绝!

    最后用的方法是:

    NSString *currentKeyboardName = [[[[UITextInputMode activeInputModes] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"isDisplayed = YES"]] lastObject] valueForKey:@"extendedDisplayName"];

    if([currentKeyboardNameisEqualToString:@"简体拼音"] || [currentKeyboardNameisEqualToString:@"表情符号"] || [currentKeyboardName isEqualToString:@"English (US)"]) {

        //系统自带键盘

        returnYES;

    }else{

        //第三方键盘

        returnNO;

    }

    如有什么不会的地方,请各位大神指出!🙏🙏🙏

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容