Removing hyper links from a URL shown in UIWebView

后端 未结 2 1649
梦如初夏
梦如初夏 2021-01-07 14:56

I am making an app which has a section for latest company news. Basically the plan was initially to use ASIHTTPRequest to extract all the HTML, search through it for tags t

相关标签:
2条回答
  • 2021-01-07 15:27

    OK, setting the UIDataDetectorTypeNone for dataDetectorTypes, should prevent the webview from detecting the links, this is correct.

    Furthermore using the

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
    

    UIWebview delegate, you can simply return NO for the URL's you don't want the user to allow following...

    Furthermore, you can insert custom CSS Rules, after the page has finished loading in the

    - (void)webViewDidFinishLoad:(UIWebView *)webView 
    

    delegate method, this way:

    [MywebView stringByEvaluatingJavaScriptFromString:@"document.styleSheets[0].addRule(\"a.link\", \"color:#FF0000\")"];
    

    So based on the CSS from the thread I mentioned, this should work:

    [MywebView stringByEvaluatingJavaScriptFromString:@"document.styleSheets[0].addRule(\".active\", \"pointer-events: none;\");document.styleSheets[0].addRule(\".active\", \"cursor: default;\")"];
    
    0 讨论(0)
  • 2021-01-07 15:28

    One thing you can also do here to further customize your links in the web view is edit the html string you are passing into the webview. Specifically, you can add CSS to the html to determine how links will be shown (colors, underlines, etc). Here is a code snippet which takes an html string and customizes it with CSS:

    NSString *HTML = [NSString stringWithFormat:@"<html>\n"
                          "<head>\n"
                          "<style type=\"text/css\">\n"
                          "body {font-family: \"%@\"; font-size: %@; color:#%@; background-color:#FFFFFF; margin: 0; padding: 0; line-height: 1.5; }\n"
                          "a:link {color:#00B0EA; text-decoration: none;}\n"
                          "</style>\n"
                          "</head>\n"
                          "<body><script type=\"text/javascript\">window.onload = function() { window.location.href = \"ready://\" + document.body.offsetHeight; } </script>%@</body>\n"
                          "</html>",
                          font.familyName, @(font.pointSize), colorHexString, htmlBodyString];
    
    0 讨论(0)
提交回复
热议问题