Decoding HTML data in swift

290 Views Asked by At

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; .....
1

There are 1 best solutions below

0
On

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