2

enter image description here I have a Url that when I run it it displays html. I need to use url to display the contents in my iOS app. How do I do that? The examples I have found use WebKit, and I don't want to use webkit because I don't want the user to have the ability to use it as a website

3
  • You should be able to block navigation on a WKWebView and you don't need to show previous page button nor URL bar. Commented May 28, 2018 at 7:22
  • What actually you want to do? do you want to display HTML String in UILabel or you want to parse HTML and display it in other controls? Commented May 28, 2018 at 7:28
  • I have edited the question. When I use url with webkit I get the purple parts that are the header and footer. I don't want the purple parts displayed. I only want the yellow parts. @Larme Commented May 28, 2018 at 8:17

1 Answer 1

1

You can use UILabel to display an attributed string parsed with your html string.

Here is the code in Swift 4

extension String {
    func toHtmlString() -> NSAttributedString? {
        guard let data = self.data(using: String.Encoding.utf16, allowLossyConversion: false) else { return nil }
        guard let html = try? NSMutableAttributedString(
            data: data,
            options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html],
            documentAttributes: nil) else { return nil }
        return html
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, please check my image above. I only want the yellow parts displayed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.