问题
App crashes when user inputs text via voice in UITextView,
Crash Method
make textview first responder by clicking on it then Press on mic button in keyboard then speech something then press Done the app crashes..
Nothing special is done with uitextview in my case..
Console Output
*
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to mutate immutable object with deleteCharactersInRange:'
First throw call stack: (0x19443c1c0 0x192e7455c 0x19443c108 0x1943d9b18 0x19a58d138 0x194e1a3c4 0x1943fd958 0x1943fb9e4 0x194e1a218 0x19aa9aac8 0x19aa9b270 0x19aa9a0b4 0x19aa97680 0x19aa979f0 0x19aa9afd0 0x19aa9b50c 0x19cdbf800 0x19cdbf064 0x19cdbf74c 0x19cdc4354 0x1005e925c 0x1005e921c 0x1005ee284 0x1943e9f2c 0x1943e7b18 0x194316048 0x195d99198 0x19a2e9628 0x19a2e4360 0x1001e03c4 0x1932f85b8) libc++abi.dylib: terminating with uncaught exception of type NSException
on further debugging i got (by entering bt in console) it gives me
* thread #1: tid = 0x645a4, 0x000000019340a014 libsystem_kernel.dylib`__pthread_kill + 8, queue =
'com.apple.main-thread', stop reason = signal SIGABRT frame #0: 0x000000019340a014 libsystem_kernel.dylib
__pthread_kill + 8 frame #1: 0x00000001934d1460 libsystem_pthread.dylib
pthread_kill + 112 frame #2: 0x000000019337e3f4 libsystem_c.dylibabort + 140 frame #3: 0x0000000192e492d4 libc++abi.dylib
abort_message + 132 frame #4: 0x0000000192e66cc0 libc++abi.dylibdefault_terminate_handler() + 304 frame #5: 0x0000000192e74844 libobjc.A.dylib
_objc_terminate() + 124 frame #6: 0x0000000192e6366c libc++abi.dylibstd::__terminate(void (*)()) + 16 frame #7: 0x0000000192e62f84 libc++abi.dylib
__cxa_throw + 136 frame #8: 0x0000000192e74690 libobjc.A.dylibobjc_exception_throw + 364 frame #9: 0x000000019443c108 CoreFoundation
+[NSException raise:format:] + 116 frame #10: 0x00000001943d9b18 CoreFoundationmutateError + 132 frame #11: 0x000000019a58d138 UIKit
-[UIScrollView description] + 104 frame #12: 0x0000000194e1a3c4 Foundation_NSDescriptionWithLocaleFunc + 108 frame #13: 0x00000001943fd958 CoreFoundation
__CFStringAppendFormatCore + 7996 frame #14: 0x00000001943fb9e4 CoreFoundation_CFStringCreateWithFormatAndArgumentsAux2 + 244 frame #15: 0x0000000194e1a218 Foundation
+[NSString stringWithFormat:] + 68 frame #16: 0x000000019aa9aac8 UIKit-[UIDictationController insertSerializedDictationResult:withCorrectionIdentifier:] + 144 frame #17: 0x000000019aa9b270 UIKit
__98-[UIDictationController finishDictationRecognitionWithPhrases:languageModel:correctionIdentifier:]_block_invoke + 464 frame #18: 0x000000019aa9a0b4 UIKit-[UIDictationController performIgnoringDocumentChanges:] + 48 frame #19: 0x000000019aa97680 UIKit
-[UIDictationController _runFinalizeOperation:] + 56 frame #20: 0x000000019aa979f0 UIKit-[UIDictationController setState:] + 612 frame #21: 0x000000019aa9afd0 UIKit
-[UIDictationController finishDictationRecognitionWithPhrases:languageModel:correctionIdentifier:] + 552 frame #22: 0x000000019aa9b50c UIKit-[UIDictationController dictationConnection:didRecognizePhrases:languageModel:correctionIdentifier:] + 204 frame #23: 0x000000019cdbf800 AssistantServices
__105-[AFDictationConnection _tellSpeechDelegateDidRecognizeSpeechPhrases:languageModel:correctionIdentifier:]_block_invoke + 80 frame #24: 0x000000019cdbf064 AssistantServices-[AFDictationConnection _dispatchCallbackGroupBlock:] + 240 frame #25: 0x000000019cdbf74c AssistantServices
-[AFDictationConnection _tellSpeechDelegateDidRecognizeSpeechPhrases:languageModel:correctionIdentifier:] + 356 frame #26: 0x000000019cdc4354 AssistantServices__101-[AFDictationConnectionServiceDelegate speechDidRecognizePhrases:usingSpeechModel:correctionContext:]_block_invoke + 76 frame #27: 0x00000001005e925c libdispatch.dylib
_dispatch_call_block_and_release + 24 frame #28: 0x00000001005e921c libdispatch.dylib_dispatch_client_callout + 16 frame #29: 0x00000001005ee284 libdispatch.dylib
_dispatch_main_queue_callback_4CF + 1200 frame #30: 0x00000001943e9f2c CoreFoundation__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12 frame #31: 0x00000001943e7b18 CoreFoundation
__CFRunLoopRun + 1660 frame #32: 0x0000000194316048 CoreFoundationCFRunLoopRunSpecific + 444 frame #33: 0x0000000195d99198 GraphicsServices
GSEventRunModal + 180 frame #34: 0x000000019a2e9628 UIKit-[UIApplication _run] + 684 frame #35: 0x000000019a2e4360 UIKit
UIApplicationMain + 208 * frame #36: 0x00000001001e03c4 motto-mobilemain(argc=1, argv=0x000000016fdd3ab8) + 124 at main.m:16 frame #37: 0x00000001932f85b8 libdyld.dylib
start + 4
Code used
@property(strong,nonatomic) UITextView *addressTextView;
- (void)viewDidLoad {
[super viewDidLoad];
addressTextView.delegate = self;
addressTextView.text = [self.dictionaryEditObject objectForKey:@"location"];
}
-(void)textViewDidBeginEditing:(UITextView *)textView
{
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
NSLog(@"textViewShouldBeginEditing:");
return YES;
}
-(void)textViewDidEndEditing:(UITextView *)textView
{
NSLog(@"textViewDidEndEditing");
}
tried on iOS: 10.2,9.3 device: iPad, iPod, iPhone 5s crash on every device
Any Help Would be appreciated Thanks
回答1:
The first thing to do is to subclass UITextView and in that subclass override func accessibilityActivate() -> Bool. Then make a delegate that handles what happens when the TextView is activated and call it through the overridden method.
Then set the delegate of the TextView based on UIAccessibilityIsVoiceOverRunning(), so if voice over is running then set the UITextViewDelegate to nil which prevents the crash from happening and then the activation action is handled by the method you overrode in the subclass above. Finally, set up a listener for UIAccessibilityVoiceOverStatusChanged and set the UITextViewDelegate to nil when going from off to on, and setting it to the original delegation class for the opposite scenario.
来源:https://stackoverflow.com/questions/44405172/app-crashes-uitextview-when-user-inputs-via-voice