(判断第三方输入法)在系统数字键盘上添加“完成”按钮引发的问题思考

       以前在系统自带的数字键盘上的左下角额外添加一个自定制的完成按钮,并且在键盘上添加了其他的控件样式。不过不久后,IOS8出来了,开始强大的支持第三方输入法。于是问题出来了。如果用户安装了第三方的输入法并设置使用。那么我们原先对于数字键盘上的控件定制将是一个很糟糕的麻烦。按照原先的数字键盘布局添加的“完成”等其他的按钮就会错位并且覆盖第三方输入法的键盘符。正常情况下实现的效果如下图:



      解决问题的办法有两个:

      第一个就是判断当前的输入法是系统的还是第三方的,然后再具体定制相应的键盘样式。但是目前官方还没有提供的公开的API来供我们获得当前键盘的类型。不过使用私有API可以获取得到当前输入法的Id。共涉及到两个私有API类:分别是,UIKeyboardInputModeControllerUIKeyboardInputModeUIKeyboardInputModeController。

创建类:UIKeyboardInputModeController

//
//  UIKeyboardInputMode.h
//  AiCai
//
//  Created by Jiabin He on 15-1-5.
//  Copyright (c) 2015年 www.AiCai.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


//
//  UIKeyboardInputMode.m
//  AiCai
//
//  Created by Jiabin He on 15-1-5.
//  Copyright (c) 2015年 www.AiCai.com. All rights reserved.
//

#import "UIKeyboardInputMode.h"

@implementation UIKeyboardInputMode

@end

      使用的地方加上下面的代码即可判断是系统输入法还是第三方的输入法   

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);
    }

     第二种方法就比较简单省事了。程序开发者可以选择禁止所有的第三方输入法在他们的应用中运行。这些应用程序使用定义在UIApplicationDelegate协议中的shouldAllowExtensionPointIdentifier: 方法(返回值为NO),从而始终使用系统输入法。

-(BOOL)application:(UIApplication *)application shouldAllowExtensionPointIdentifier:(NSString *)extensionPointIdentifier{
    return NO;
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值