[UIDictationConnection cancelSpeech] Keyboard Dictation Error

In our crash analytics, there was a weird error that we couldn't reproduce it is part of textView that opens the keyboard and uses the dictation functionality to speak text and it will input it on the textView.

I tried to debug it and tried to investigate but it seems this issue is more on IOS internal but it cause crash in our application.

please can someone gave my idea to fix this or can you provide any information that this might a OS bug using keyboard dictation.

I have experienced the same issue. The top of the crash stack trace is

__37-[UIDictationConnection cancelSpeech]_block_invoke

Any information/help would be appreciated.

Let me ask if your problem has been solved?

Let me ask if your problem has been solved?

I’m still i don't have any solution for this. even on the lates version 17.3.1 we got reported issue hoping to find an answer on this. It is hard to replicate i tried many time to test keyboard dictation

hoping there is answer for this issue

Reason:

This crash was caused by multiple threads accessing the lastHypothesis function of the UIDictationController, causing a wild pointer problem

Solution:

Lock the setLastHypothesis and lastHypothesis functions of UIDictationController,


// hd_hookMethod is hook components similar to aspect

[NSClassFromString(@"UIDictationController") hd_hookMethod:NSSelectorFromString(@"setLastHypothesis:") option:HDHookOptionInstead handle:^(HDInvocation *invocation){
        
        [[invocation.target hdcore_lock] lock];
        [invocation invoke];
        [[invocation.target hdcore_lock] unlock];

    } error:nil];
    
    [NSClassFromString(@"UIDictationController") hd_hookMethod:NSSelectorFromString(@"lastHypothesis") option:HDHookOptionInstead handle:^NSString * (HDInvocation *invocation){
        [[invocation.target hdcore_lock] lock];

        __autoreleasing NSString *orgHypothesis;
        [invocation invokeWithReturnValue:&orgHypothesis];
        orgHypothesis = orgHypothesis.mutableCopy;

        [[invocation.target hdcore_lock] unlock];
        return orgHypothesis;
    } error:nil];

The solution comes from the HuolalaTech team, I just translated it into English

Reference material:

https://juejin.cn/post/7396463744186515465

[UIDictationConnection cancelSpeech] Keyboard Dictation Error
 
 
Q