开发者

Removing hyper links from a URL shown in UIWebView

开发者 https://www.devze.com 2023-04-13 04:51 出处:网络
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

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.


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;\")"];


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号