I have tried all the solution available on Stack-Overflow Couldn't helped out by any solution
extension ViewController: WKNavigationDelegate,WKUIDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.insertContentsOfCSSFile2(into: webView)
self.insertContentsOfCSSFile1(into: webView)
}
func insertContentsOfCSSFile2(into webView: WKWebView) {
guard let path = Bundle.main.path(forResource: "article-style", ofType: "css") else { return }
let cssString = try! String(contentsOfFile: path).trimmingCharacters(in: .whitespacesAndNewlines)
let jsString = "var style = document.createElement('style'); style.innerHTML = '\(cssString)'; document.head.appendChild(style);"
print(jsString)
webView.evaluateJavaScript(jsString) {(result, error) in
if let error = error {
print(error)
}
}
}
func insertContentsOfCSSFile1(into webView: WKWebView) {
guard let path = Bundle.main.path(forResource: "custom", ofType: "css") else { return }
let cssString = try! String(contentsOfFile: path).trimmingCharacters(in: .whitespacesAndNewlines)
let jsString = "var style = document.createElement('style'); style.innerHTML = '\(cssString)'; document.head.appendChild(style);"
print(jsString)
webView.evaluateJavaScript(jsString) { (result, error) in
if let error = error {
print(error)
}
if let result = result {
print(result)
}
}
}
}
Here is the web View initialisation
func loadUrl() {
webView = WKWebView(frame: .zero, configuration: WKWebViewConfiguration())
webView.navigationDelegate = self
webView.allowsLinkPreview = true
webView.uiDelegate = self
webViewContainer.addSubview(webView)
self.addConstraints(to: webView, with: webViewContainer)
do {
guard let filePath = Bundle.main.path(forResource: "article", ofType: "html")
else {
print ("FILE READING ERROR")
return
}
let contents = try String(contentsOfFile: filePath, encoding: .utf8)
let baseUrl = URL(fileURLWithPath: filePath)
print(baseUrl)
print(contents)
webView.loadHTMLString(htmlString, baseURL:URL(fileURLWithPath: Bundle.main.bundlePath))
}
catch {
print ("FILE HTML ERROR")
}
}
"article.html " , "article-style.css" and "custom.css" are the three local file in my project directory
I am calling the webView load request from viewDidLoad() method
There is nothing that I haven't tried but couldn't solved my problem
Any help is greatly appriciated