Tometoyou
Tometoyou

Reputation: 8406

How to allow emojis in UITextView URL

I have an attributed string that detects hashtags and adds a link attribute for each hashtag of hash:[word]. This string is then displayed inside a UITextView and is detected as a link. This works fine for words with normal characters, however, if there's an emoji it crashes when trying to press the link. Usually it will call textView(_:shouldInteractWith:in:), but it crashes before it even calls that.

Unfortunately Xcode doesn't provide any specific lines that the code crashes on, however the stack track looks like this:

#0  0x000000010ff4ad55 in static DateComponents._unconditionallyBridgeFromObjectiveC
(NSDateComponents?) -> DateComponents ()
#1  0x000000010c2214df in @objc MyTableTextCell.textView(UITextView, shouldInteractWith : 
URL, in : _NSRange) -> Bool ()
#2  0x0000000110d88282 in -[_UITextViewInteractableLink allowInteraction:] ()
#3  0x0000000110d86f22 in -[_UITextViewInteractableItem handleTap] ()
#4  0x0000000110d86df4 in -[UITextView(LinkInteraction)
 validateInteractionWithLinkAtPoint:] ()
#5  0x0000000110749f8b in -[UITextInteractionAssistant(UITextInteractionAssistant_Internal)
 linkTapRecognizer:] ()
#6  0x0000000110733289 in -[UIGestureRecognizerTarget _sendActionWithGestureRecognizer:] ()

How can I make the clicks on the emoji hashtags work? I've tried percentage escaping it, but it doesn't seem to do anything.

Upvotes: 1

Views: 450

Answers (1)

SuperDuperTango
SuperDuperTango

Reputation: 1408

This is a little old, but the URL tap handling code will crash in the location you provided if it gets characters that aren't allowable via URL encoding (e.g. emojis). So for example, when manually creating a NSAttributedString with a NSLinkAttributedName, make sure the value is URLEncoded.

When the UITextView does link detection, it will already do this correctly for you.

Upvotes: 1

Related Questions