Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing app to use Apple Keyboard in iOS 8

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 experience so please don't discuss that point with me :)

I know I can set the securedEntry property to true to force the Apple keyboard (http://www.imore.com/custom-keyboards-ios-8-explained). Maybe iOS 8 will let me set this property and NOT mask the text?

like image 832
Jake Stout Avatar asked Aug 06 '14 20:08

Jake Stout


2 Answers

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 :)

like image 168
Joe Tam Avatar answered Oct 24 '22 07:10

Joe Tam


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

like image 20
Albert Renshaw Avatar answered Oct 24 '22 09:10

Albert Renshaw