【问题标题】:Forcing app to use Apple Keyboard in iOS 8强制应用在 iOS 8 中使用 Apple 键盘
【发布时间】:2014-09-29 22:04:36
【问题描述】:

如何让我的应用程序的 UITextfields 仅在 iOS 8 中使用 Apple 键盘?我不想让第三方键盘,句号。我知道这可能是糟糕的用户体验,所以请不要和我讨论这一点:)

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

【问题讨论】:

    标签: ios security uitextfield ios8 ios-app-extension


    【解决方案1】:

    Apple 为此提供了一个 API。把这个放在你的AppDelegate

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

    这是最干净且记录在案的方法:)

    【讨论】:

    • 我试过了,但即使它返回 NO,第 3 方键盘仍然显示。
    • 你可能没有把它放在你的AppDelegate.m中。
    【解决方案2】:

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

    很遗憾,这也会隐藏按键信息,并覆盖和禁用自动大写字母以及自动更正和拼写建议。因此,在强制用户使用 Apple 键盘完成后,您必须切换回 NO

    打开然后关闭此属性可以强制 Apple 键盘成为默认显示的第一个键。


    现在,用户仍然可以按全局键,因此您有两个选择:

    •一个,你可以让他们检测他们是否使用[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(inputModeDidChange:) name:@"UITextInputCurrentInputModeDidChangeNotification" object:nil];,然后重新循环上面的secureTextEntry代码并强制切换回Apple默认键盘(不专业的方法,但很容易编程)。 (*注意!这也将阻止用户使用听写键盘(单击空格键旁边的麦克风图标按钮),除非您使用未记录的代码来检测它是否是听写(确实存在并且应该已经通过Apple 验证了几个帐户。有关此信息可以找到 here)


    要么


    •二:使用 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
    











    这是该代码的示例...我使用的是自定义键盘,当我启动应用程序时,它强制我使用 Apple 键盘,然后在“更改键盘”按钮上放置一个红色方块,这使得我无法单击按钮来更改键盘。 (当然,红色方块可以更改为任何东西,例如空白键或处于褪色(禁用)状态的地球图标。)



    【讨论】:

    • 感谢您的回复。我的团队决定只使用自定义键盘并相信苹果的安全性。
    • 祝你好运! :)
    【解决方案3】:

    据我所知,没有办法做到这一点。但是,如果您愿意付出努力,您可以创建自己的自定义输入视图来模仿标准 Apple 键盘,并使用它来代替。有关输入视图的更多信息,请参阅documentation

    【讨论】:

    • 感谢您的回复。如果没有人提出实现此目的的方法,我会将您的答案标记为已接受。
    • @barnone 为解决您的问题提供了非常有创意的工作...花了我几个小时才想出,所以请检查一下 :) 我什至发布了您需要的所有演示代码!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-09
    • 1970-01-01
    • 2014-03-17
    • 2015-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多