App crashes with error "Fatal Exception: NSInvalidArgumentException -[<NSTextContentStorage: 0x281dc68a0> locationFromLocation:withOffset:] received invalid location (null)"

From IOS 16 onwards my app which uses UITextView in iOS for editing, started crashing for a number of users with error.

App crashes with error "Fatal Exception: NSInvalidArgumentException -[<NSTextContentStorage: 0x281dc68a0> locationFromLocation:withOffset:] received invalid location (null)"

After researching we found that this could be an issue with the new textkit engine of UITextView.

And to restore the previous behavior and avoid the crash, we explicitly set the UITextView to use TextKit 1 and re-released the app with Xcode 14.0.

But unfortunately, even after this change, this crash is not gone and users are still facing this issue.

Adding a link that suggests a response from the Apple developers team to change textkit engine from textkit 2 to textkit 1 for this issue.

https://lapcatsoftware.com/articles/textview5.html

I'm also adding complete stack trace of this crash


Fatal Exception: NSInvalidArgumentException 0 CoreFoundation 0x9e88 __exceptionPreprocess

1 libobjc.A.dylib 0x178d8 objc_exception_throw

2 CoreFoundation 0xf9c58 __CFDictionaryCreateGeneric

3 UIFoundation 0x220e0 -[NSTextContentStorage locationFromLocation:withOffset:]

4 UIFoundation 0x9f104 -[NSTextLineFragment textLineFragmentRange]

5 UIFoundation 0x86e70 __95-[NSTextLayoutManager _copyTextLineFragmentRangeForPoint:inContainerAtLocation:pointPlacement:]_block_invoke

6 CoreFoundation 0xa3a0 NSARRAY_IS_CALLING_OUT_TO_A_BLOCK

7 CoreFoundation 0x910e0 -[__NSArrayI enumerateObjectsWithOptions:usingBlock:]

8 UIFoundation 0x86cfc -[NSTextLayoutManager _copyTextLineFragmentRangeForPoint:inContainerAtLocation:pointPlacement:]

9 UIFoundation 0x86eb4 -[NSTextLayoutManager lineFragmentRangeForPoint:inContainerAtLocation:]

10 UIFoundation 0x7e6ec -[NSTextSelectionNavigation _lineFragmentInfoForPoint:inContainerAtLocation:bounds:layoutOrientation:beforeLineFragment:afterLineFragment:lineFragmentRange:]

11 UIFoundation 0x815d8 -[NSTextSelectionNavigation textSelectionsInteractingAtPoint:inContainerAtLocation:anchors:modifiers:selecting:bounds:]

12 UIKitCore 0x98f5d8 -[_UITextKit2LayoutController cursorPositionAtPoint:inContainer:]

13 UIKitCore 0xf33d88 -[UITextInputController _characterPositionForPoint:]

14 UIKitCore 0xf4a2cc -[UITextView closestPositionToPoint:]

15 UIKitCore 0xedef5c -[UITextSelectionInteraction tappedToPositionCursorWithGesture:atPoint:granularity:completionHandler:]

16 UIKitCore 0xedecc8 -[UITextSelectionInteraction _checkForRepeatedTap:gestureLocationOut:]

17 UIKitCore 0xedf524 -[UITextSelectionInteraction _handleMultiTapGesture:]

18 UIKitCore 0x24ed54 -[UIApplication sendAction:to:from:forEvent:]

19 UIKitCore 0x9448fc -[UITextMultiTapRecognizer onStateUpdate:]

20 UIKitCore 0xbe874 -[UIGestureRecognizerTarget _sendActionWithGestureRecognizer:]

21 UIKitCore 0x42c9f0 _UIGestureRecognizerSendTargetActions

22 UIKitCore 0x1a60a4 _UIGestureRecognizerSendActions

23 UIKitCore 0x86a14 -[UIGestureRecognizer _updateGestureForActiveEvents]

24 UIKitCore 0x1328d8 _UIGestureEnvironmentUpdate

25 UIKitCore 0x9b7e14 -[UIGestureEnvironment _deliverEvent:toGestureRecognizers:usingBlock:]

26 UIKitCore 0xf731c -[UIGestureEnvironment _updateForEvent:window:]

27 UIKitCore 0xfbc74 -[UIWindow sendEvent:]

28 UIKitCore 0xfaf44 -[UIApplication sendEvent:]

29 Aspen 0x6963c0 TimerApplication.sendEvent(_:) ()

30 Aspen 0x696834 @objc TimerApplication.sendEvent(_:) ()

31 UIKitCore 0xfa600 __dispatchPreprocessedEventFromEventQueue

Please let us know how can we fix this issue, app was working fine sine IOS 11.0 but suddenly it has started crashing.

Currently users are also facing this issue in updated IOS version IOS 16.1.1 Device - iPhone 12 , iPhone 12 Pro, iPhone 13

Facing to the same issue

Same Issue.

Same issue

Same here and I control any text location. FYI my textview is embedded into a UITableViewCell. Crash happen when the textview become the first responder.

@GrayFox293 does your textview has scrolling disabled or enabled?

same here

Same issue. Is anybody able to reproduce this? What steps are needed, or which actions are done by the users?

The same here, I'm yet to reproduce it. In production crash rate is 0.1% and all crashes are NSInvalidArgumentException: -[<NSTextContentStorage: 0x280b4b670> locationFromLocation:withOffset:] received invalid location (null).

We just ran into this as well and had to add this to all of our UITextViews:

// HACK: Force TextKit 1 because TextKit 2 is so buggy... let _ = self.textView.layoutManager

Same issue

Same issue. Is anybody able to reproduce this?

Same issue QQ

Solved this by keeping the scrolling enabled.

If you are using UITextView inside cell and want to achieve dynamic resizing toggle is isScrollEnabled on didEndEditing and didBeginEditing. . . . f.e.

textView.rx
			.didBeginEditing
			.asDriverIgnoringErrors()
			.drive(onNext: { [unowned self] in
				if textView.text == placeholder {
					textView.text = ""
					textView.textColor = textColor
					textView.isScrollEnabled = false
				}
			})

textView.rx
			.didEndEditing
			.asDriverIgnoringErrors()
			.drive(onNext: { [unowned self] in
				if textView.text.isEmpty {
					textView.text = placeholder
					textView.textColor = placeholderColor
					textView.isScrollEnabled = true
				}
			})
			.disposed(by: disposeBag)
App crashes with error "Fatal Exception: NSInvalidArgumentException -[&lt;NSTextContentStorage: 0x281dc68a0&gt; locationFromLocation:withOffset:] received invalid location (null)"
 
 
Q