Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set html string in webview (swift 4.0)

Tags:

html

swift

xcode9

I am facing problem, when i am trying to load this type of html string

(<p>Identify the arrow-marked structures in the images<img alt=\"\" src=\"https:\/\/dams-apps-production.s3.ap-south-1.amazonaws.com\/course_file_meta\/73857742.PNG\" \/><\/p>\r\n)

in webview. problem is raised due to backward slaces.

Any Help will Appreciated

like image 592
Pintu Rajput Avatar asked Mar 16 '18 07:03

Pintu Rajput


2 Answers

URL string has a problem with forward slash(es) / in your html string.

enter image description here

Correct url string is: https://dams-apps-production.s3.ap-south-1.amazonaws.com/course_file_meta/73857742.PNG

Here I tried this and its working:

class WebKitController: UIViewController {

    @IBOutlet weak var webView: WKWebView!

    override func viewDidLoad() {
       super.viewDidLoad()
       loadHTMLStringImage()
    }

    func loadHTMLStringImage() -> Void {
        let htmlString = "<p>Identify the arrow-marked structures in the images<img alt=\"\" src=\"https://dams-apps-production.s3.ap-south-1.amazonaws.com/course_file_meta/73857742.PNG\"></p>"
        webView.loadHTMLString(htmlString, baseURL: nil)
    }
}

Result:

enter image description here

like image 101
Krunal Avatar answered Oct 11 '22 01:10

Krunal


If the back-slashes are your problem, you should remove them:

let origString = ...
let unescapedString = origString.replacingOccurrences(of: "\\", with: "")
webview.loadHTMLString(unescapedString, baseURL:nil)
like image 21
Andreas Oetjen Avatar answered Oct 11 '22 03:10

Andreas Oetjen