針對某個 UITextField 禁用第三方鍵盤

在日常開發(fā)中,可能某個 UITextField 只能輸入數(shù)字,但是因為安裝了第三方鍵盤(搜狗、百度等)會受到影響,需要禁用第三方鍵盤。

禁用第三方鍵盤需要在 AppDelegate.m 中實現(xiàn)以下代碼

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

這樣就全局禁用了第三方鍵盤,下面針對某個 UITextField 進行禁用第三方鍵盤

對某個 UITextField 禁用第三方鍵盤

  1. UITextField 創(chuàng)建一個分類,然后在分類的 .h 文件中添加一個實例對象屬性 yxc_usingSystemKeyboard 和一個類對象屬性 yxc_globalUsingSystemKeyboard

    @property (nonatomic, assign) BOOL yxc_usingSystemKeyboard;  /**< 使用系統(tǒng)鍵盤 */
    @property (nonatomic, assign, class) BOOL yxc_globalUsingSystemKeyboard;   /**< 全局是否使用系統(tǒng)鍵盤,主動設(shè)置無效 */
    
  2. 關(guān)聯(lián)屬性

    - (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. 監(jiān)聽 UITextFieldTextDidBeginEditingNotificationUITextFieldTextDidEndEditingNotification 通知

     [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(textFieldDidBeginEdit:)
                                                 name:UITextFieldTextDidBeginEditingNotification
                                               object:self];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(textFieldDidEndEdit:)
                                                 name:UITextFieldTextDidEndEditingNotification
                                               object:self];
    
  4. 在監(jiān)聽到 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 實例對象設(shè)置 yxc_usingSystemKeyboard 屬性值為 YES 后就對某個 UITextField 禁用了第三方鍵盤.

    效果:

針對某個輸入框禁用第三方鍵盤效果.gif

在這里,第一個輸入框默認(rèn)的樣式,不禁用第三方鍵盤,第二個輸入框禁用第三方鍵盤并且設(shè)置鍵盤樣式為 UIKeyboardTypeNumberPad

代碼

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容