-1

I am implementing showing html data received from server in web view in swift ... I ma getting html data from server in following format (just part of data string)

I want to convert this to proper html format so that I can load it in webview using

webview.loadHTMLString(html_data, baseURL: nil)

any idea how to convert following input data into proper html tags

\\n\\u003Chtml\\u003E\\n\\u003Chead\\u003E\\n\\u003Cstyle type=\\u0022text\\/css\\u0022\\u003E\\n\\t#one_page .logo-jobbers {\\n  \\t\\tbackground: url(\\u0022..\\/images\\/logo-jobbers.png\\u0022) no-repeat;\\n  \\t\\tdisplay: block;\\n  \\t\\t\\/*float: left;*\\/\\n  \\t\\theight: 72px;\\n  \\t\\tmargin: 11px auto 11px auto;\\n  \\t\\twidth: 232px;\\n  \\t\\t-webkit-transition: all 0.3s;\\n  \\t\\t-moz-transition: all 0.3s;\\n  \\t\\t-ms-transition: all 0.3s;\\n  \\t\\t-o-transition: all 0.3s;\\n  \\t\\ttransition: all 0.3s;\\n\\t}\\n\\n\\t#one_page .h1_home {\\n    \\tcolor: #243D54 !important;\\n\\t\\tposition: relative;\\n\\t\\tfont-family: \\u0022lato\\u0022,sans-serif;\\n    \\tfont-size: 34px;\\n    \\tfont-weight: 300; .....
2
  • There may be a conversion problem : your html may be utf encoded with character escaping . Look there you may find a way to convert your html data to string. Commented Mar 24, 2022 at 12:24
  • Your data looks fine. The printout of the data is only a representation (escaping the escape characters). Commented Mar 24, 2022 at 12:47

1 Answer 1

-1

I manage to decode the string

            let htmldata : Data = (body.data(using: String.Encoding.utf8))!
        let decoder = JSONDecoder()
        do {
            let product = try decoder.decode(String.self, from: htmldata)
            print(product)
            webview.loadHTMLString(product, baseURL: nil)
        }
        catch {
        }

Also refers this link https://developer.apple.com/documentation/foundation/jsondecoder

Sign up to request clarification or add additional context in comments.

Comments

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.