Using now the preferred WKWebView, you can put the external css file, say tweaks.css in the root of your project or in a subfolder, then you can enter it on your page as follows:
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { guard let path = Bundle.main.path(forResource: "tweaks", ofType: "css") else { return } let css = try! String(contentsOfFile: path).replacingOccurrences(of: "\\n", with: "", options: .regularExpression) let js = "var style = document.createElement('style'); style.innerHTML = '\(css)'; document.head.appendChild(style);" webView.evaluateJavaScript(js) }
To make a file available:
- Click on your project.
- Choose a target
- Assembly phase selection
- Expand Copy Resources
- Press "+" and select a file.
Also make sure your controller implements WKNavigationDelegate:
class ViewController: UIViewController, WKNavigationDelegate
source share