Removing Hyperlinks from URLs Displayed in UIWebView

I am making an application that has a section for the latest company news. Basically, the plan was originally used to use ASIHTTPRequest to extract all the HTML, search through it for tags, then display information from the headline and description of the news, and then display it as a simple outline of the text as a scroll of my application. However, this turned out to be a nightmare, as the site has nothing to do with how the information is displayed.

Since then, I decided to display the URL in the UIWebView, which shows the latest company news. I want this to be a one-page view, so my user cannot go to other aspects of the site. Is it possible to stop site hyperlinks displayed as links? My code for UIWebView is as follows:

UIView *webNewsView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; self.view = webNewsView; CGRect webFrame = [[UIScreen mainScreen] applicationFrame]; webFrame.origin.y = 0.0f; webNews = [[UIWebView alloc] initWithFrame:webFrame]; webNews.backgroundColor = [UIColor clearColor]; webNews.scalesPageToFit = YES; webNews.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); webNews.delegate = self; [self.view addSubview: webNews]; [webNews loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://myURLHere"]]]; indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; indicator.frame = CGRectMake(0.0, 0.0, 25.0, 25.0); indicator.center = self.view.center; [self.view addSubview: indicator]; 

Any help would be greatly appreciated. Thanks

EDIT: I also tried adding the following:

 webNews.dataDetectorTypes = UIDataDetectorTypeNone; 

Which has nothing to do with link detection.

0
source share
2 answers

OK, setting UIDataDetectorTypeNone for dataDetectorTypes should prevent web browsing from detecting links, that’s right.

Also using

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

UIWebview divider, you can simply return NO for a URL that you do not want the user to allow the following ...

Alternatively, you can embed custom CSS rules after the page has finished loading in

 - (void)webViewDidFinishLoad:(UIWebView *)webView 

delegate a method this way:

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

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

 [MywebView stringByEvaluatingJavaScriptFromString:@"document.styleSheets[0].addRule(\".active\", \"pointer-events: none;\");document.styleSheets[0].addRule(\".active\", \"cursor: default;\")"]; 
+3
source

One thing you can also do here to further customize your links in the web view is to edit the html line that you submit to the web view. In particular, you can add CSS to html to determine how links will be displayed (colors, underlines, etc.). Here is a snippet of code that takes an html string and customizes it using 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]; 
+1
source

All Articles