强制应用程序在iOS 8中使用Apple Keyboard

如何让我的应用程序的UITextfields只使用iOS 8中的苹果键盘? 我不想让第三方键盘,期间。 我知道这可能是不好的用户体验,所以请不要与我讨论这一点:)

我知道我可以将securedEntry属性设置为true以强制Apple键盘( http://www.imore.com/custom-keyboards-ios-8-explained )。 也许iOS 8会让我设置这个属性,而不是屏蔽文本?

通过将“ Secure Text Entry YES的UITextView或UITextField属性设置为YES ,自定义键盘将不会默认显示,也不能切换到。

不幸的是,这也会隐藏按键信息以及覆盖和禁用自动上限和自动更正和拼写build议。 所以你必须切换回到NO后,强迫使用户使用苹果键盘。

切换此属性,然后closures可以强制Apple键盘默认显示的第一个键。


现在,用户仍然可以按全局键,所以你有两个select:

一个,你可以让他们检测是否使用[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(inputModeDidChange:) name:@"UITextInputCurrentInputModeDidChangeNotification" object:nil]; 只需重新启用上面的secureTextEntry代码,并强制切换回Apple默认的键盘(非专业方法,但非常容易编程)。 (*注意!这也将阻止用户使用听写键盘(单击空格键旁边的麦克风图标button),除非您使用未logging的代码来检测是否听写(这是否存在并且已经通过苹果validation一些帐户。关于这方面的信息可以在这里find)

要么

•二:使用UIWindows获取默认键盘的ONTOP,并添加一个UIWindow,将userInteractionEnabled设置为YES覆盖键位置的位置(这将需要一些条件语句来确保覆盖正确的“更改键盘键”为每一种可能性(即风景键盘iPhone4,人像键盘iPhone5等)。

这是一些演示代码虽然它的肖像键盘(iphone5和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 


这是一个代码将看起来像一个例子…我使用的是自定义键盘,当我启动应用程序,它强迫我的苹果键盘,然后把一个红色的方块上的“更改键盘”button,这使得我不可能点击button来改变键盘。 (红色方块当然可以变成任何一个空白键或处于褪色(禁用)状态的地球图标。)

在这里输入图像说明

苹果提供了一个完全的API。 把它放在你的AppDelegate

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

这是最干净和logging的方式来做到这一点:)

据我所知,没有办法做到这一点。 但是,如果您愿意付出努力,则可以创build自己的模拟标准Apple键盘的自定义input视图,然后使用它。 有关input视图的更多信息,请参阅文档 。