Swift & UIWebView element to hide

I have a UIWebView and am loading such content

let url = NSURL (string: "http://www.myresponsivesite.fr/section/");
    let requestObj = NSURLRequest(URL: url!);
    webView.loadRequest(requestObj);

It works fine, but I would like some divs and other elements to appear on the display. So my question is: how can I add some css from my application with fast? Thanks.

+2
source share
2 answers

I finally found another way: I load my html into a variable, and I replace some div with a “display: none” example:

 let urlDeBase = "http://mywebsite/section/"    
    if let url = NSURL (string: urlDeBase){
        var error: NSError?
        let myHTML = String(contentsOfURL: url, encoding: NSUTF8StringEncoding, error: &error)

and I will replace what I want

 let mystring = myHTML?.stringByReplacingOccurrencesOfString("<h3 class=\"myclass\">Menu</h3>", withString: "<h3 class=\"myclass\" style=\"display:none\">Menu</h3>")
 webvieww.loadHTMLString(mystring, baseURL: nil)
+2
source

I found a couple of posts about this, and Apple's docs, here , were one solution:

, , -, Swift:

var loadStyles = "var script = 
  document.createElement('link');
  script.type = 'text/css';
  script.rel = 'stylesheet';
  script.href = 'http://fake-url/styles.css';
  document.getElementsByTagName('body')[0].appendChild(script);"

website.stringByEvaluatingJavaScriptFromString(loadStyles)

: Swift stringByEvaluatingJavaScriptFromString

Apple docs.

+1

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


All Articles