App crashes when pressing link on UITextView
As mentioned in this Apple Forum post, I implemented the following UITextViewDelegate
and it solved my issue
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool
{
UIApplication.sharedApplication().openURL(URL)
return false
}
@Louis Tur: Thanks for the link
Swift 5.2
@available(iOS 10.0, *)
func textView(_ textView: UITextView, shouldInteractWith url: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
{
UIApplication.shared.openURL(url)
return false
}
@available(iOS, deprecated: 10.0)
func textView(_ textView: UITextView, shouldInteractWith url: URL, in characterRange: NSRange) -> Bool {
{
UIApplication.shared.openURL(url)
return false
}
My fix for the crash was to implement both the deprecated and its replacement delegate methods.
/// Gets called if iOS version is >= 10.
@available(iOS 10.0, *)
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
return textViewShouldInteractWithURL(URL: URL)
}
/// deprecated delegate method. Gets called if iOS version is < 10.
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
return textViewShouldInteractWithURL(URL: URL)
}
func textViewShouldInteractWithURL(URL: URL) -> Bool {
// common logic here
}