In daily development, aUITextField
Only 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.m
The 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 aUITextField
To disable the third-party keyboard
For aUITextField
Disable third-party keyboard
-
to
UITextField
Create a category, and then in the category Add an instance object attribute to the H fileyxc_usingSystemKeyboard
And 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*/
-
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]; }
-
monitor
UITextFieldTextDidBeginEditingNotification
andUITextFieldTextDidEndEditingNotification
notice[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidBeginEdit:) name:UITextFieldTextDidBeginEditingNotification object:self]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidEndEdit:) name:UITextFieldTextDidEndEditingNotification object:self];
-
Listening to
UITextFieldTextDidBeginEditingNotification
andUITextFieldTextDidEndEditingNotification
Notification pairyxc_globalUsingSystemKeyboard
Value modification- (void)textFieldDidBeginEdit:(NSNotification *)notification { UITextField.yxc_globalUsingSystemKeyboard = self.yxc_usingSystemKeyboard; } - (void)textFieldDidEndEdit:(NSNotification *)notification { UITextField.yxc_globalUsingSystemKeyboard = NO; }
-
AppDelegate
inapplication: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; }
By
UITextField
Instance object settingsyxc_usingSystemKeyboard
Attribute value isYES
And then to someoneUITextField
Third party keyboards are disabledeffect:

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