Disable third-party keyboard for a uitextfield

Time:2022-6-10

In daily development, aUITextFieldOnly numbers can be entered, but the installation of a third-party keyboard (Sogou, Baidu, etc.) will be affected, so the third-party keyboard needs to be disabled.

Disabling the third-party keyboard requiresAppDelegate.mThe following code is implemented in

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

In this way, the third-party keyboard is disabled globally. For aUITextFieldTo disable the third-party keyboard

For aUITextFieldDisable third-party keyboard

  1. toUITextFieldCreate a category, and then in the category Add an instance object attribute to the H fileyxc_usingSystemKeyboardAnd a class object attributeyxc_globalUsingSystemKeyboard

    @property (nonatomic, assign) BOOL yxc_ usingSystemKeyboard;  /**<  Using the system keyboard*/
    @property (nonatomic, assign, class) BOOL yxc_ globalUsingSystemKeyboard;   /**<  Whether the system keyboard is used globally. The active setting is invalid*/
  2. Association attribute

    - (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. monitorUITextFieldTextDidBeginEditingNotificationandUITextFieldTextDidEndEditingNotificationnotice

     [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(textFieldDidBeginEdit:)
                                                 name:UITextFieldTextDidBeginEditingNotification
                                               object:self];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(textFieldDidEndEdit:)
                                                 name:UITextFieldTextDidEndEditingNotification
                                               object:self];
    
  4. Listening toUITextFieldTextDidBeginEditingNotificationandUITextFieldTextDidEndEditingNotificationNotification pairyxc_globalUsingSystemKeyboardValue modification

    - (void)textFieldDidBeginEdit:(NSNotification *)notification {
        
        UITextField.yxc_globalUsingSystemKeyboard = self.yxc_usingSystemKeyboard;
    }
    
    - (void)textFieldDidEndEdit:(NSNotification *)notification {
        
        UITextField.yxc_globalUsingSystemKeyboard = NO;
    }
    
  5. AppDelegateinapplication:shouldAllowExtensionPointIdentifier:Modified in method

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

    ByUITextFieldInstance object settingsyxc_usingSystemKeyboardAttribute value isYESAnd then to someoneUITextFieldThird party keyboards are disabled

    effect:

Disable third-party keyboard for a uitextfield

Disables third-party keyboard effects for an input field gif

Here, the default style of the first input box is not to disable the third-party keyboard. The second input box disables the third-party keyboard and sets the keyboard style toUIKeyboardTypeNumberPad

code

Recommended Today

Modul of fastems

Each module of fastems is implemented from the abstract class Fastems.Mms.Client.Infrastructure.UiModuleBase; public class DataManagerModule : UiModuleBase { public override void Initialize() { AddResourceDictionary(“/Resources/DataManagerResources.xaml”, typeof(DataManagerModule)); RegisterViewWithRegion(“DialogRegion”, typeof(DialogView)); RegisterViewWithRegion(“BusyIndicatorRegion”, typeof(BusyIndicatorView)); } } And Fastems.Mms.Client.Infrastructure.UiModuleBase inherits from Fastems.Mms.Client.Infrastructure.ModuleBase public abstract class UiModuleBase : ModuleBase { [Import] public IRegionManager RegionManager { get; set; } [Import] public IMergedDictionaryRegistry MergedDictionaryRegistry { […]