针对某个 UITextField 禁用第三方键盘

在日常开发中,可能某个 UITextField 只能输入数字,但是因为安装了第三方键盘(搜狗、百度等)会受到影响,需要禁用第三方键盘。

禁用第三方键盘需要在 AppDelegate.m 中实现以下代码

- (BOOL)application:(UIApplication *)application
shouldAllowExtensionPointIdentifier:(NSString *)extensionPointIdentifier
{
    if ([extensionPointIdentifier isEqualToString:@"com.apple.keyboard-service"]) {
        return NO;
    }
    return YES;
}

这样就全局禁用了第三方键盘,下面针对某个 UITextField 进行禁用第三方键盘

对某个 UITextField 禁用第三方键盘

  1. UITextField 创建一个分类,然后在分类的 .h 文件中添加一个实例对象属性 yxc_usingSystemKeyboard 和一个类对象属性 yxc_globalUsingSystemKeyboard

    @property (nonatomic, assign) BOOL yxc_usingSystemKeyboard;  /**< 使用系统键盘 */
    @property (nonatomic, assign, class) BOOL yxc_globalUsingSystemKeyboard;   /**< 全局是否使用系统键盘,主动设置无效 */
    
  2. 关联属性

    - (void)setYxc_usingSystemKeyboard:(BOOL)yxc_usingSystemKeyboard {
        
        objc_setAssociatedObject(self, @selector(yxc_usingSystemKeyboard), @(yxc_usingSystemKeyboard), OBJC_ASSOCIATION_ASSIGN);
    }
    
    - (BOOL)yxc_usingSystemKeyboard {
        
        NSNumber *yxc_usingSystemKeyboard = objc_getAssociatedObject(self, @selector(yxc_usingSystemKeyboard));
        return [yxc_usingSystemKeyboard boolValue];
    }
    
    + (void)setYxc_globalUsingSystemKeyboard:(BOOL)yxc_globalUsingSystemKeyboard {
        
        objc_setAssociatedObject(self, @selector(yxc_globalUsingSystemKeyboard), @(yxc_globalUsingSystemKeyboard), OBJC_ASSOCIATION_ASSIGN);
    }
    
    + (BOOL)yxc_globalUsingSystemKeyboard {
        
        NSNumber *yxc_globalUsingSystemKeyboard = objc_getAssociatedObject(self, @selector(yxc_globalUsingSystemKeyboard));
        return [yxc_globalUsingSystemKeyboard boolValue];
    }
    
  3. 监听 UITextFieldTextDidBeginEditingNotificationUITextFieldTextDidEndEditingNotification 通知

     [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(textFieldDidBeginEdit:)
                                                 name:UITextFieldTextDidBeginEditingNotification
                                               object:self];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(textFieldDidEndEdit:)
                                                 name:UITextFieldTextDidEndEditingNotification
                                               object:self];
    
  4. 在监听到 UITextFieldTextDidBeginEditingNotificationUITextFieldTextDidEndEditingNotification 通知对 yxc_globalUsingSystemKeyboard 值修改

    - (void)textFieldDidBeginEdit:(NSNotification *)notification {
        
        UITextField.yxc_globalUsingSystemKeyboard = self.yxc_usingSystemKeyboard;
    }
    
    - (void)textFieldDidEndEdit:(NSNotification *)notification {
        
        UITextField.yxc_globalUsingSystemKeyboard = NO;
    }
    
  5. AppDelegateapplication:shouldAllowExtensionPointIdentifier: 方法中修改

    - (BOOL)application:(UIApplication *)application shouldAllowExtensionPointIdentifier:(NSString *)extensionPointIdentifier
     {
      if ([extensionPointIdentifier isEqualToString:@"com.apple.keyboard-service"]) {
          if (UITextField.yxc_globalUsingSystemKeyboard) {
              return NO;
          }
      }
      return YES;
    }
    

    通过对某个 UITextField 实例对象设置 yxc_usingSystemKeyboard 属性值为 YES 后就对某个 UITextField 禁用了第三方键盘.

    效果:

针对某个输入框禁用第三方键盘效果.gif

在这里,第一个输入框默认的样式,不禁用第三方键盘,第二个输入框禁用第三方键盘并且设置键盘样式为 UIKeyboardTypeNumberPad

代码

你可能感兴趣的:(针对某个 UITextField 禁用第三方键盘)