0

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.

Joan Cardona
  • 3,463
  • 2
  • 25
  • 43
  • Your code is strange because you use `NSURL`, wouldn't it much better to use `URL` in Swift (if available, which it is in your version), and for legals, you set the value a URL, and the terms, you set the value as a String. Are both not working? – Larme Jul 01 '20 at 08:41
  • @Larme about the string and the NSURL, they both should work (and work in iOS 13). I did different way because I was debugging trying to see if passing the NSURL fixed the issue, but it didn't. Sorry, I know it's confusing. – Joan Cardona Jul 02 '20 at 09:20
  • There was a bug in NSAttributedString, I don't remember the iOS impacted version, but check if you add an "invisible" effect applied to all the attributedString and check if it works. – Larme Jul 02 '20 at 09:22

0 Answers0