Download UIWebView using local CSS file

I am looking for a way to load my UIWebView using a local CSS file which should affect the loadRequest UIWebView.

For clarity: I have a UIWebView and I upload it with the website URL. I also have a local CSS file that should affect the loadRequest URL.

I want to load this CSS file into a UIWebView.

Thanks,

+6
source share
3 answers

There is one or two answers on this question fooobar.com/questions/893860 / ... that can help you.

You need to load local CSS (using a method different from using @Shrey but looking for your CSS file) and somehow embed it on the page, and the only way seems to be to use:

[webview stringByEvaluatingJavaScriptFromString:someJavascriptToInjectCSS]; 

and some clever Javascript to change the page to add CSS.

Hope this helps you in the right direction. I used this method to inject material into the pages, so it really works, but I don't know Javascript well enough to write code to inject CSS into the page.

+3
source

This is a version of Swift.

Upload the css file to the delegate method webViewDidFinishLoad and use stringByEvaluatingJavaScriptFromString . Loans here

  func loadWebViewStyles() { guard let cssPath = NSBundle.mainBundle().pathForResource("theme", ofType: "css") else { return } let loadStyles = "var script = document.createElement('link'); script.type = 'text/css'; script.rel = 'stylesheet'; script.href = '\(cssPath)'; document.getElementsByTagName('body')[0].appendChild(script);" webView.stringByEvaluatingJavaScriptFromString(loadStyles) } 
0
source

try the following:

[WebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];

-1
source

Source: https://habr.com/ru/post/893859/


All Articles