Thursday, 27 September 2018

Displaying HTML text in UITextView crash occurred in swift 4?

I am using this code snipped

Code to show HTML Text in TextView

var htmlToAttributedString: NSAttributedString? {
    guard let data = data(using: .utf8) else { return NSAttributedString() }
    do {
        return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:String.Encoding.utf8.rawValue], documentAttributes: nil) // Get crash on this line
    } catch let error {
        print(error.localizedDescription)
        return NSAttributedString()
    }
}

var htmlToString: String {
    return htmlToAttributedString?.string ?? ""
}

showing HTML text in UITableViewCell

cell.textViewMessage.attributedText = msg.htmlToAttributedString

Launching first time there is no crash but after that when I run the code got a crash and not working after that.

Thread 1: EXC_BAD_ACCESS (code=1, address=0x10)

#Edit HTML String to display in cell

<p>Here\'s a short video tour.  Press play to start.</p><br><iframe class=\"ql-video\" frameborder=\"0\" allowfullscreen=\"true\" src=\"https://www.youtube.com\"></iframe><br>

#Edit 1 - I am trying to run this code in Playground and it's just working fine except now it's showing an error. Please see the attached image

Playground output



from Displaying HTML text in UITextView crash occurred in swift 4?

No comments:

Post a Comment