Forcing app to use Apple Keyboard in iOS 8

前端 未结 3 1312
一整个雨季
一整个雨季 2020-12-30 12:37

How can I get my app\'s UITextfields to only use the Apple keyboard in iOS 8? I do not want to allow third party keyboards, period. I understand it may be bad user experienc

相关标签:
3条回答
  • 2020-12-30 13:10

    By setting a UITextView or UITextField's property of Secure Text Entry to YES, custom keyboards will not be shown by default and can also not be toggled to.

    This will also, unfortunately, hide key press information as well as over-ride and disable auto caps and auto correct and spelling suggestions. So you have to toggle is back off to NO after you're done forcibly making the user use Apple keyboard.

    Toggling this property on and then off can force the Apple Keyboard to be the first key that displays by default.


    NOW, the user will still be able to press the global key so you have two options:

    •One, you can just let them and detect if they do using [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(inputModeDidChange:) name:@"UITextInputCurrentInputModeDidChangeNotification" object:nil]; and just reloop the secureTextEntry code above and force switch back to Apple default keyboard (unprofessional method, but very easy to program). (*Note! This will also prevent users from being able to use the dictation keyboard (clicking the microphone icon button next to the space bar), unless you use undocumented code to detect if it's dictation or not (which does exist and has supposedly passed Apple validation on a few accounts. Info on this can be found here)


    Or


    •Two: to use UIWindows to get ONTOP of the default keyboard and add a UIWindow with userInteractionEnabled set to YES covering where that key is location (this will take a few conditional statements to make sure you're covering the right "change keyboard key" for every possibility. (i.e. landscape keyboard iPhone4, portrait keyboard iPhone5, etc).

    Here is some demo code though of it working for portrait keyboards (iphone5 and iphone4)






    viewController.h

    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController <UITextFieldDelegate> {
    
        UITextField *theTextField;
        UIWindow *statusWindow;
    
    }
    
    @end
    





    viewController.m

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
    
        theTextField = [[UITextField alloc] initWithFrame:CGRectMake(50, 50, 50, 250)];
        [theTextField becomeFirstResponder];
        theTextField.secureTextEntry = YES;//turn back OFF later (like in `viewDidAppear`) and reset textField properties to YES (like auto correct, auto caps, etc).
        theTextField.delegate = self;
        [self.view addSubview:theTextField];
    
    
    
        //UIWindow *statusWindow; MUST be defined in .h file!
        statusWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        statusWindow.frame = CGRectMake(37, self.view.frame.size.height-47, 45, 45);
        statusWindow.windowLevel = UIWindowLevelStatusBar;
        statusWindow.hidden = NO;
        statusWindow.backgroundColor = [UIColor clearColor];
    
        UIButton *keyboardCover = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 45, 45)];
        keyboardCover.backgroundColor = [UIColor redColor];
        [statusWindow addSubview:keyboardCover];
    
        //statusWindow.alpha = 1.00;
        statusWindow.userInteractionEnabled = YES;
        [statusWindow makeKeyAndVisible];
    
    
    }
    
    -(void)viewDidAppear:(BOOL)animated {
        [theTextField resignFirstResponder];
        theTextField.secureTextEntry = NO;
        theTextField.autocorrectionType = 2;//ON
        theTextField.autocapitalizationType = 2;//ON
        theTextField.spellCheckingType = 2;//ON
        [theTextField becomeFirstResponder];
    }
    
    
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    












    Here is an example of what that code will look like... I was using a custom keyboard, when I launched the app it forced me over to the Apple keyboard, then put a red square over the "change keyboard" button, which made it impossible for me to click the button to change the keyboard. (the red square can be changed to be anything of course like a blank key or the globe icon in a faded (disabled) state.)



    enter image description here

    0 讨论(0)
  • 2020-12-30 13:13

    Apple provides an API for exactly that. Put this in your AppDelegate

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

    This is the cleanest and documented way to do it :)

    0 讨论(0)
  • 2020-12-30 13:23

    To the best of my knowledge, there is no way to do this. However, if you're willing to put in the effort, you could create your own custom input view that mimics the standard Apple keyboard, and use that instead. For more on input views, refer to the documentation.

    0 讨论(0)
提交回复
热议问题