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

前端 未结 5 2079
说谎
说谎 2020-11-29 02:44

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 acc

相关标签:
5条回答
  • 2020-11-29 02:53

    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.

    0 讨论(0)
  • 2020-11-29 03:01

    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];
        }
    }
    
    0 讨论(0)
  • 2020-11-29 03:05

    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.

    0 讨论(0)
  • 2020-11-29 03:07

    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;
    
    0 讨论(0)
  • 2020-11-29 03:15

    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
    }
    
    0 讨论(0)
提交回复
热议问题