如何判断是否是第三方输入法

发布网友 发布时间:2022-04-22 00:29

我来回答

2个回答

热心网友 时间:2023-06-23 21:18

从键盘事件(比如UIKeyboardWillShowNotification)中,得不到使用是哪个输入法。只能得到类似下面的信息:

{
UIKeyboardAnimationCurveUserInfoKey = 7;
UIKeyboardAnimationDurationUserInfoKey = 0;
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 568}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 460}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 568}, {320, 0}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 352}, {320, 216}}";
}

所以感觉使用正常渠道可能得不到这个信息,不过使用私有API可以获取得到当前输入法的Id:

UIKeyboardInputModeController *key = [UIKeyboardInputModeController sharedInputModeController];

//当前输入法
UIKeyboardInputMode *currentInputMode = [key currentInputMode];

//第三方扩展输入法
NSArray *extensionInputModes = [key extensionInputModes];

if ([extensionInputModes containsObject:currentInputMode]) {
NSLog(@"current input mode (%@) is the 3rd party input mode", currentInputMode.identifier);
} else {
NSLog(@"current input mode (%@) is build-in input mode", currentInputMode.identifier);
}

输出大概是这样的:

current input mode (com.cleanshavenapps.Pasteboard.PasteboardKeyboard) is the 3rd party input mode
current input mode (com.sogou.sogouinput.basekeyboard) is the 3rd party input mode
current input mode (com..inputMethod.keyboard) is the 3rd party input mode
current input mode (en_US@hw=US;sw=QWERTY) is build-in input mode
current input mode (zh_Hans-Pinyin@sw=Pinyin10-Simplified;hw=US) is build-in input mode
current input mode (emoji@sw=Emoji) is build-in input mode

有三个是系统自带的,三个是第三方的。

共涉及到两个私有API类:分别是,UIKeyboardInputModeController 和 UIKeyboardInputMode。

这是我的头文件,你放入工程就可以用了:

//
// UIKeyboardInputMode.h
// TestDeterminKeyboard
//
// Created by Joey on 10/30/14.
// Copyright (c) 2014 Joeyio.com. All rights reserved.
//

#ifndef TestDeterminKeyboard_UIKeyboardInputMode_h
#define TestDeterminKeyboard_UIKeyboardInputMode_h

@class NSString, NSArray;

@interface UIKeyboardInputMode : 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

@interface UIKeyboardInputModeController : 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:(unsigned int)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

热心网友 时间:2023-06-23 21:18

在应用中我们常常自定义键盘,特别是为数字键盘增加新的功能按钮;现在越来越多的输入法支持iphone,百度。腾讯,搜狐等;
如用户安装了第三方的输入法,我们就需要辨别出来:

在IOS 4.2以上支持 UITextInputMode,可通过 [[UITextInputMode currentInputMode] primaryLanguage] 来获取当前输入法,如果是自定义的这里会显示自定义信息(例如:。。)但是如何更好的识别呢,暂时也没有找到令我满意的方式:

需要知道的:
普通的数字键盘弹出时,打印[[UITextInputMode currentInputMode] primaryLanguage] 跟当前语言相关,比如当前语言为en,打印所得是en-US;当前语言是zhhhans,打印所得是zh-hans;若改数字键盘是密码键盘形式,打印所得都是en-US;

暂时可以通过以下方式解决:
1、获取系统设置里当前语言,例如ja(日语),可通过判断primaryLanguage 是否为en-US或者primaryLanguage是否包含前缀ja(注:primaryLanguage 与当前语言并不完全相同);若不满足前两个条件,则应该是第三方输入法;(这种方式也可能会有问题,加入除了一个输入法,名字是japan,谁知道呢??)

2、[UITextInputMode activeInputModes],另外一个规律是,如果是系统输入法获取的这个数组中有包含一个keyboardmode对象,而出现第三方输入法键盘时,包含两个keyboardmode对象,不知道算不算规律,暂时来看也是一种方法吧;

暂时采用的第一种方式,感觉更有依据性一些,如果有更好的方法,欢迎分享

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com