0

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 then pull the information out for news title and description then display it as just plan text in a scroll view of my app. However, this was proving to be a nightmare as the site has no pattern to the way the information is displayed.

I have since decided to display the URL in a UIWebView which shows the latest company news. I want this to be a one page view so my user cannot navigate to other aspects of the site. Is it possible to stop the sites hyperlinks showing up as links? My code for the 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 appreciated. Thanks

EDIT: I have also tried adding the following;

webNews.dataDetectorTypes = UIDataDetectorTypeNone;

Which makes no difference to the detection of links.

Paul Morris
  • 1,763
  • 10
  • 33
  • 47

2 Answers2

3

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;\")"];
Lefteris
  • 14,550
  • 2
  • 56
  • 95
  • Setting UIDataDetectorTypeNone made no difference bizarrely. All the links were still clickable. Even unchecking the box through interface builder made no difference. I will try your final piece of advice tonight, thank you. – Paul Morris Oct 17 '11 at 09:11
  • You surely can disable the links from being clickable with the shouldStartLoadWithRequest delegate. As for disabling the links from showing up as links, it's up to using the correct CSS rules, based on the way I showed in the above example. Maybe there's a better CSS approach, however I'm not that good in CSS – Lefteris Oct 17 '11 at 09:28
  • I have implemented [webNews stringByEvaluatingJavaScriptFromString:@"document.styleSheets[0].addRule(\"a.link\", \"color:#FF0000\")"]; in the - (void)webViewDidFinishLoad:(UIWebView *)webView delegate method and it makes no difference. all links are still clickable – Paul Morris Oct 17 '11 at 19:59
  • I didn't say they will not be clickable! Did the color of the links change to red? If yes, then the CSS rules work. So this means you need to simply set the link to have the same color as the rest of the text, and it shouldn't be underlined... Besides you should be able to cancel the load requests from the web delegate I showed you! – Lefteris Oct 17 '11 at 21:49
  • As for the correct CSS look at this thread: http://stackoverflow.com/questions/2091168/disable-a-link-using-css : .active { pointer-events: none; cursor: default; } – Lefteris Oct 17 '11 at 21:56
1

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];
Brian Sachetta
  • 3,319
  • 2
  • 34
  • 45