Siri Dictation Bypasses UITextView:shouldChangeTextInRange Delegate

Seb*_*nik 12 objective-c uitextview siri dictation

我用来(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)对每个输入的字符进行实时正则表达式验证UITextView.

这很好用,甚至可以处理可能包含无效字符的粘贴文本.

但是,Siri似乎并不关心这些事情并将其直接转发(void)textViewDidChange:(UITextView *)theTextView,绕过shouldChangeTextInRange代表并避免验证检查.

这对我来说似乎是一个安全漏洞,并打破了每个其他输入通道所遵循的API流程.

有关如何让Siri Dictation遵守呼叫的任何想法shouldChangeTextInRange

And*_*ett 0

我认为这就是答案......如果:

 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
Run Code Online (Sandbox Code Playgroud)

被调用那么它是一个用户输入。因此,在其中放入一个 BOOL 来记住是否检查了“shouldChange”。

然后,麦克风输入将调用:

-(void)textViewDidChange:(UITextView *)textView
Run Code Online (Sandbox Code Playgroud)

检查这里的 BOOL,如果为 NO,则为麦克风输入。

(在textViewDidChange末尾设置BOOL = NO:)

这对我来说非常有用。