I want to inject CSS by using URL link ,
Working
Working below code, If I store CSS file in Bundle path of project,
lazy var webView: WKWebView = {
guard let path = Bundle.main.path(forResource: "style", ofType: "css") else {
return WKWebView()
}
let cssString = try! String(contentsOfFile: path).components(separatedBy: .newlines).joined()
let source = """
var style = document.createElement('style');
style.innerHTML = '\(cssString)';
document.head.appendChild(style);
"""
let preferences = WKPreferences()
preferences.setValue(true, forKey:"developerExtrasEnabled")
let userScript = WKUserScript(source: source,
injectionTime: .atDocumentEnd,
forMainFrameOnly: true)
let userContentController = WKUserContentController()
userContentController.addUserScript(userScript)
let configuration = WKWebViewConfiguration()
configuration.userContentController = userContentController
configuration.preferences = preferences
let webView = WKWebView(frame: .zero,
configuration: configuration)
webView.navigationDelegate = self
webView.scrollView.isScrollEnabled = false
webView.scrollView.bounces = false
return webView
}()
Expectation
I have CSS file which is store in our server having some path link let say ,
So I want to apply style.css file by using URL link ,
Please help me to apply CSS file by using URL only , I don't want to store it in bundle , bundle CSS file is already working for me , our CSS style will change dynamically so I want apply URL link CSS file.