简体   繁体   中英

Disable Dictation button on the keyboard of iPhone 4S / new iPad

Ours is a health care app. We have a HIPAA-compliant speech recognizer in the app through which all the dictation can take place. The hospitals don't want physicians to accidentally start speaking to the Nuance Dragon server which is not HIPAA-compliant. So, I was looking for ways I could supress the dictation key on the keyboard.

I tried putting a fake button on the Dictation button on the key pad, but on the iPad the split dock concept keeps moving the microphone all over the screen. This does not sound like a reasonable solution. Are there any experts out there who could help me?

OKAY, finally got it! The trick is to observe UITextInputMode change notifications, and then to gather the identifier of the changed mode (Code seems to avoid the direct use of Private API, though seems to require a little knowledge of private API in general), and when the mode changes to dictation, resignFirstResponder (which will cancel the voice dictation). YAY! Here is some code:

Somewhere in your app delegate (at least that's where I put it)

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

And then you can

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];
    }
}

you can create your own keyboard and set the inputView for the text fields that will accept this dictation. then when they press any keys they will get your keyboard, therefore you dont have to override the keys on the standard keyboard, you will be able to customize the entire thing.

self.myButton.inputView = self.customKeyboardView;

here is an example of an extremely custom keyboard

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

Ray also has a teriffic tutorial on custom keyboards.

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

I hope that helps.

I had the same issue and the only way i found that hides the dictation button is changing the keyboard type. For me changing it to email type seemed to be reasonable:

textField.keyboardType = UIKeyboardTypeEmailAddress;

You could make a subclass of UITextField/UITextView that overrides insertDictationResult: to not insert anything.

This won't prevent the information being sent, but you could then display an alert informing them of the breech.

This is a Swift 4 solution based on @BadPirate's hack. It will trigger the initial bell sound stating that dictation started, but the dictation layout will never appear on the keyboard.

This will not hide the dictation button from your keyboard: for that the only option seems to be to use an email layout with UIKeyboardType.emailAddress .


In viewDidLoad of the view controller owning the UITextField for which you want to disable dictation:

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

Then the custom callback:

@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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM