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

前端 未结 5 2114
说谎
说谎 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 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
    }
    

提交回复
热议问题