Sanket Pund Sanket Pund - 1 month ago 4
CSS Question

I am facing a weird issue with UIWebview object in IOS

I m facing a weird issue with UIWebview object. here i have to load html page with css file and js.here is the below lines of code i used

let htmlFile = Bundle.main.path(forResource: "sale", ofType: "html")
let html = try? String(contentsOfFile: htmlFile!,encoding:String.Encoding.utf8)
webViewObj.loadHTMLString(html!,baseURL: nil)

it only loads the html page on device and IOS simulator (no css and JS applied ).

Where as the same html file loads well on Android device with applied css and JS.

Please suggest any workaround.

Answer

Please check your sale.html, the css file should write like this:

<link href="mycss.css" rel="stylesheet" type="text/css">

Pay attention to the "href".

Add baseurl when load html source:

let htmlFile = Bundle.main.path(forResource: "sale", ofType: "html")
let baseURL = NSURL.fileURL(withPath: Bundle.main.bundlePath)
let html = try? String(contentsOfFile: htmlFile!,encoding:String.Encoding.utf8)
webViewObj.loadHTMLString(html!,baseURL: baseURL)

If it not work, you can use Safari to debug the sale.html. Or you can print the path of sale.html and open the path in Finder. Just figure out where the css and js file, then change the "href" to the right path.

You may be want to see this: https://webdesign.tutsplus.com/articles/quick-tip-using-web-inspector-to-debug-mobile-safari--webdesign-8787