NSAttributedString.Key,Link not working on ios 11.4

655 Views Asked by At

I have a UITextView that works great in iOS 12 and 13 but not on iOS 11.4 and below.

This textView has a text that 2 parts need to be clickable and open a webview (the common Privacy Policy and Terms & Conditions text).

let attributedLegalString = NSMutableAttributedString(string: "sign_up_terms_of_use".localized())
        if let rangePrivacy = "sign_up_terms_of_use".localized().lowercased().rangeOfSubstring("profile_privacy_policy".localized().lowercased()) {
            attributedLegalString.addAttribute(NSAttributedString.Key.link, value: NSURL(string: "https://XXX/privacy-policy")!, range: rangePrivacy)
            attributedLegalString.addAttribute(NSAttributedString.Key.font, value: AppFontName.fontType.H1.value, range: rangePrivacy)
        }
        if let rangeTerms = "sign_up_terms_of_use".localized().lowercased().rangeOfSubstring("login_terms_of_use".localized().lowercased()) {
            attributedLegalString.addAttribute(NSAttributedString.Key.link, value: "https://XXX/terms-conditions", range: rangeTerms)
            attributedLegalString.addAttribute(NSAttributedString.Key.font, value: AppFontName.fontType.caption.value, range: rangeTerms)
        }

Then I have this method:

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        self.openWebsite(url: URL)
        return false
    }

    func openWebsite(url: URL) {
        let safariVC = SFSafariViewController(url: url)
        self.present(safariVC, animated: true, completion: nil)
        safariVC.delegate = self
    }

This works great except for iOS 11. Any clue?

The textView data Detector is selected for Link. And the change of color works fine so the Range is correct.

0

There are 0 best solutions below