首页 > iOS8下,有没有API能够知道当前输入法是否第三方输入法吗

iOS8下,有没有API能够知道当前输入法是否第三方输入法吗

通过官方的方法能否得知用户目前使用的输入法是否第三方输入法


从键盘事件(比如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.baidu.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类:分别是,UIKeyboardInputModeControllerUIKeyboardInputMode

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

//
//  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

没有试过,不过你可以看看UITextField或者UITextView的inputView,通过查看他们的class也许可以知道


To ask the system to switch to another keyboard, call the advanceToNextInputMode method (..) The system picks the appropriate “next” keyboard; there is no API to obtain a list of enabled keyboards or for picking a particular keyboard to switch to.

从上述苹果的正式文档当中描述,是没有一个列表来获取的。

【热门文章】
【热门文章】