iOS15 UITextView After dragging the view, it causes a crash, Invalid parameter not satisfying: pos
Asked Answered
K

2

6

crash example

The above article is a problem found by a netizen on the forum. Recently, I also found a similar crash stack on firebase. I guess it is also caused by this reason, and it may be the problem of data out of bounds. Do you have a good solution?

Firebase crashed the stack:

Fatal Exception: NSInternalInconsistencyException
Invalid parameter not satisfying: pos
0
CoreFoundation
__exceptionPreprocess
1
libobjc.A.dylib
objc_exception_throw
2
Foundation
_userInfoForFileAndLine
3
UIKitCore
-[_UITextKitTextPosition compare:]
4
UIKitCore
-[UITextInputController comparePosition:toPosition:]
5
UIKitCore
-[UITextView comparePosition:toPosition:]
6
UIKitCore
-[UITextPasteController _clampRange:]
7
UIKitCore
__87-[UITextPasteController _performPasteOfAttributedString:toRange:forSession:completion:]_block_invoke
8
UIKitCore
__87-[UITextPasteController _performPasteOfAttributedString:toRange:forSession:completion:]_block_invoke.177
9
UIKitCore
-[UITextInputController _pasteAttributedString:toRange:completion:]
10
UIKitCore
-[UITextPasteController _performPasteOfAttributedString:toRange:forSession:completion:]
11
UIKitCore
__49-[UITextPasteController _executePasteForSession:]_block_invoke
12
libdispatch.dylib
_dispatch_call_block_and_release
13
libdispatch.dylib
_dispatch_client_callout
14
libdispatch.dylib
_dispatch_main_queue_callback_4CF
15
CoreFoundation
__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__
16
CoreFoundation
__CFRunLoopRun
17
CoreFoundation
CFRunLoopRunSpecific
18
GraphicsServices
GSEventRunModal
19
UIKitCore
-[UIApplication _run]
20
UIKitCore
UIApplicationMain
21
HelloTalk_Binary
main.m -  20 
main + 20
Kurtis answered 14/10, 2021 at 9:44 Comment(1)
Got exact crash also when just pasting text onto UITextView on iOS 15. Answer from @Sibcat also fixed it.Bautzen
R
7

I could not find the reason, but found work around. If I implement one of the two methods of UITextPasteDelegate for UITextView, textPasteConfigurationSupporting or performPasteOf, to return string without any attributes, then app does not crash.

public func textPasteConfigurationSupporting(
  _ textPasteConfigurationSupporting: UITextPasteConfigurationSupporting,
  performPasteOf attributedString: NSAttributedString,
  to textRange: UITextRange
) -> UITextRange {
  let start = textView.offset(from: textView.beginningOfDocument, to: textRange.start)
  let length = textView.offset(from: textRange.start, to: textRange.end)
  let nsRange = NSRange(location: start, length: length)

  let shouldInsert = textView(
   textView,
   shouldChangeTextIn: nsRange,
   replacementText: attributedString.string
  )

  if shouldInsert {
   textView.replace(textRange, withText: attributedString.string)
  }

  return textRange
}

or

public func textPasteConfigurationSupporting(
  _ textPasteConfigurationSupporting: UITextPasteConfigurationSupporting,
  combineItemAttributedStrings itemStrings: [NSAttributedString],
  for textRange: UITextRange
) -> NSAttributedString {
  return NSAttributedString(string: itemStrings.map { $0.string }.joined())
}
Rote answered 19/10, 2021 at 8:57 Comment(2)
Thanx! Implementing func textPasteConfigurationSupporting( _ textPasteConfigurationSupporting: UITextPasteConfigurationSupporting, performPasteOf attributedString: NSAttributedString, to textRange: UITextRange ) -> UITextRange work for me! (another one don't) don't forget about textView.pasteDelegate = selfKraut
There is no crash in iOS iOS 15.5 and iOS 16, is this fixed in a specific iOS 15.x?Infeasible
C
1

@Sibcat has a good catch.

The documentations for the method textPasteConfigurationSupporting:performPasteOfAttributedString:toRange: says that if you implement this method - no system mechanizm be applied (where the crash occurs)

It this is not implemented, the standard paste mechanism will be used. So just implementing this method the app is protected from the crash.

Additional note

The crash occurs only if you return NO as answer from delegate method textView:shouldChangeTextInRange:replacementText:text

Added:

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    BOOL success = NO;
    
    NSString *newString = [textView.text stringByReplacingCharactersInRange:range withString:text];
    
    if([self isNumericString:newString] || [newString isEqualToString:@""])
    {
        success = [newString floatValue] <= maxValue
    }
    else
    {
        if ([text isEqualToString:@"\n"])
        {
            [textView resignFirstResponder];
        }
    }
    
    return success;
}
Crosslink answered 4/11, 2021 at 21:53 Comment(3)
Can you share the source used for "The crash occurs only if you return NO as answer from delegate method textView:shouldChangeTextInRange:replacementText:text". please?Margaritamargarite
@Margaritamargarite Sorry for the late response. The added sample just returns NO when the input goes to be not numeric.Crosslink
There is no crash in iOS iOS 15.5 and iOS 16, is this fixed in a specific iOS 15.x?Infeasible

© 2022 - 2024 — McMap. All rights reserved.