禁用iPhone 4S /新iPad键盘上的听写按钮

问题描述 投票:20回答:5

我们是一个医疗保健应用程序。我们在应用程序中有一个符合HIPAA标准的语音识别器,通过它可以进行所有的听写。医院不希望医生不小心开始与不符合HIPAA标准的Nuance Dragon服务器对话。所以,我一直在寻找可以在键盘上按下听写键的方法。

我尝试在键盘上的Dictation按钮上放置一个假按钮,但在iPad上,分离式底座概念使麦克风在整个屏幕上移动。这听起来不是一个合理的解决方案。那里有专家可以帮助我吗?

iphone objective-c ios ipad
5个回答
14
投票

好的,终于明白了!诀窍是观察UITextInputMode更改通知,然后收集更改模式的标识符(代码似乎避免直接使用私有API,虽然似乎需要一点私有API的一点知识),并且模式更改时to dictation,resignFirstResponder(将取消语音听写)。好极了!这是一些代码:

在您的应用代理中的某个地方(至少我放在哪里)

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(inputModeDidChange:) name:@"UITextInputCurrentInputModeDidChangeNotification"
                                           object:nil];

然后就可以了

UIView *resignFirstResponder(UIView *theView)
{
    if([theView isFirstResponder])
    {
        [theView resignFirstResponder];
        return theView;
    }
    for(UIView *subview in theView.subviews)
    {
        UIView *result = resignFirstResponder(subview);
        if(result) return result;
    }
    return nil;
}

- (void)inputModeDidChange:(NSNotification *)notification
{        
    // Allows us to block dictation
    UITextInputMode *inputMode = [UITextInputMode currentInputMode];
    NSString *modeIdentifier = [inputMode respondsToSelector:@selector(identifier)] ? (NSString *)[inputMode performSelector:@selector(identifier)] : nil;

    if([modeIdentifier isEqualToString:@"dictation"])
    {
        [UIView setAnimationsEnabled:NO];
        UIView *resigned = resignFirstResponder(window);
        [resigned becomeFirstResponder];
        [UIView setAnimationsEnabled:YES];

        UIAlertView *denyAlert = [[[UIAlertView alloc] initWithTitle:@"Denied" message:nil delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil] autorelease];
        [denyAlert show];
    }
}

10
投票

您可以创建自己的键盘并为接受此听写的文本字段设置inputView。然后当他们按任意键时他们将获得你的键盘,因此你不必覆盖标准键盘上的键,你将能够自定义整个事物。

self.myButton.inputView = self.customKeyboardView;

这是一个极其自定义键盘的示例

http://blog.carbonfive.com/2012/03/12/customizing-the-ios-keyboard/

Ray还有一个关于自定义键盘的很棒的教程。

http://www.raywenderlich.com/1063/ipad-for-iphone-developers-101-custom-input-view-tutorial

我希望有所帮助。


7
投票

我有同样的问题,我发现隐藏听写按钮的唯一方法是改变键盘类型。对我来说,将其更改为电子邮件类型似乎是合理的:

textField.keyboardType = UIKeyboardTypeEmailAddress;

1
投票

您可以创建UITextField / UITextView的子类来覆盖insertDictationResult:不插入任何内容。

这不会阻止发送信息,但您可以显示警告,告知他们臀部。


0
投票

这是基于@ BadPirate的hack的Swift 4解决方案。它将触发初始铃声,说明听写开始,但听写布局将永远不会出现在键盘上。

这不会隐藏键盘上的听写按钮:因为唯一的选择似乎是使用UIKeyboardType.emailAddress的电子邮件布局。


在视图控制器的viewDidLoad中拥有你要禁用听写的UITextField

// Track if the keyboard mode changed to discard dictation
NotificationCenter.default.addObserver(self,
                                       selector: #selector(keyboardModeChanged),
                                       name: UITextInputMode.currentInputModeDidChangeNotification,
                                       object: nil)

然后是自定义回调:

@objc func keyboardModeChanged(notification: Notification) {
    // Could use `Selector("identifier")` instead for idSelector but
    // it would trigger a warning advising to use #selector instead
    let idSelector = #selector(getter: UILayoutGuide.identifier)

    // Check if the text input mode is dictation
    guard
        let textField = yourTextField as? UITextField
        let mode = textField.textInputMode,
        mode.responds(to: idSelector),
        let id = mode.perform(idSelector)?.takeUnretainedValue() as? String,
        id.contains("dictation") else {
            return
    }

    // If the keyboard is in dictation mode, hide
    // then show the keyboard without animations
    // to display the initial generic keyboard
    UIView.setAnimationsEnabled(false)
    textField.resignFirstResponder()
    textField.becomeFirstResponder()
    UIView.setAnimationsEnabled(true)

    // Do additional update here to inform your
    // user that dictation is disabled
}
© www.soinside.com 2019 - 2024. All rights reserved.