iOS13适配:三指撤销和文案限长

4,364 阅读2分钟

在适配iOS13的过程中,UITextField输入中文的时候三指撤销产生了 crash。

Bugly报错

NSInternalInconsistencyException
setGroupIdentifier:: _NSUndoStack 0x1206532f0 is in invalid state, calling setGroupIdentifier with no begin group mark

堆栈信息

0 CoreFoundation	___exceptionPreprocess + 220
1 libobjc.A.dylib	objc_exception_throw + 56
2 Foundation	-[_NSUndoStack groupIdentifier]
3 Foundation	-[NSUndoManager undoNestedGroup] + 240
4 UIKitCore	-[UIUndoGestureInteraction undo:] + 72
5 UIKitCore	-[UIKBUndoInteractionHUD performDelegateUndoAndUpdateHUDIfNeeded] + 96
6 UIKitCore	-[UIKBUndoInteractionHUD controlActionUpInside:] + 152
7 UIKitCore	-[UIApplication sendAction:to:from:forEvent:] + 96
8 xxxxx	-[UIApplication(MemoryLeak) swizzled_sendAction:to:from:forEvent:] + 288
9 UIKitCore	-[UIControl sendAction:to:forEvent:] + 240
10 UIKitCore	-[UIControl _sendActionsForEvents:withEvent:] + 408
11 UIKitCore	-[UIControl touchesEnded:withEvent:] + 520
12 UIKitCore	-[UIWindow _sendTouchesForEvent:] + 2324
13 UIKitCore	-[UIWindow sendEvent:] + 3352
14 UIKitCore	-[UIApplication sendEvent:] + 336
15 UIKitCore	___dispatchPreprocessedEventFromEventQueue + 5880
16 UIKitCore	___handleEventQueueInternal + 4924
17 UIKitCore	___handleHIDEventFetcherDrain + 108
18 CoreFoundation	___CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
19 CoreFoundation	___CFRunLoopDoSource0 + 80
20 CoreFoundation	___CFRunLoopDoSources0 + 180
21 CoreFoundation	___CFRunLoopRun + 1080
22 CoreFoundation	CFRunLoopRunSpecific + 464
23 GraphicsServices	GSEventRunModal + 104
24 UIKitCore	UIApplicationMain + 1936
25 xxxxx	main + 148
26 libdyld.dylib	_start + 4

问题定位

没有太多思路的时候,通过注释代码,最终定位到了问题所在。

[self addTarget:observer
         action:@selector(textChange:)
forControlEvents:UIControlEventEditingChanged];
- (void)textChange:(UITextField *)textField {
    ... ... 
    UITextRange *selectedRange = [textField markedTextRange];
    if (!selectedRange || !selectedRange.start) {
        if (destText.length > maxLength) {
            textField.text = [destText substringToIndex:maxLength];
        }
    }
}

这段代码在输入的时候会限制文案的长度。三指撤销会触发UIControlEventEditingChanged事件,执行textChange,此时获取到的markedTextRangenil,即便是存在markedText。这就导致UITextFieldtext有可能会被修改。修改文案后再继续执行撤销操作,必定会产生 crash。

解决方案

将文案判长和截取异步添加到主队列,在下一个runloop执行。

- (void)textChange:(UITextField *)textField {
    dispatch_async(dispatch_get_main_queue(), ^{
        ... ...
    });
}

数字截断后 crash

数字输入限制长度后,超过长度后继续输入,这个时候撤销也会产生crash,而且上面的方法不可行。目前想到的方案是在UITextField的回调方法进行输入的拦截。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    /// 输入数字后截取字符串仍旧可以触发撤销操作导致crash, 在这里拦截一下
    if (textField.keyboardType == UIKeyboardTypeNumberPad
        && range.location >= textField.tt_maxLength) {
            return NO;
    }
    return YES;
}