4

I need to remove the hyperlinks from the URL shown in UIWebView and I have looked at this question: Removing hyper links from a URL shown in UIWebView.

I know I need to use this method:

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

But I still seem to have some problems.

Firstly, how do i avoid only certain links (for example: www.google.com).

Next, how do i avoid all links in my UIWebView?

My code looks like this:

[webUI loadHTMLString:[strDescription stringByDecodingHTMLEntities] baseURL:nil];
webUI.dataDetectorTypes = UIDataDetectorTypeNone;

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSLog(@"finish loading");

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

}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
       return YES;
}

Need some guidance. Thanks..

The HTML string looks like this:

> <div style="font-family: Helvetica"><div style="color: #FFFFFF;"><div
> style="font-family: Helvetica;"><p><span style="font-size:
> 24px;"><strong>Optimal Performance Always</strong></span><span
> style="font-size: 18px;"><br /></span></p><p><span style="font-size:
> 18px;">The standard servicing package<a
> href="http://www.google.com">www.google.com</a></div>
Community
  • 1
  • 1
lakshmen
  • 28,346
  • 66
  • 178
  • 276

1 Answers1

3

If you want to disable all links after the first page was loaded you can add a property to store if the page was loaded and use its value on webView:shouldStartLoadWithRequest:

@property(nonatomic) BOOL pageLoaded; // initially NO

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSLog(@"finish loading");

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

    // after all your stuff
    self.pageLoaded = YES;
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
   return ! self.pageLoaded;
}

Note that this doesn't hide links, it only makes the webview not load them.

Also, you can check request.URL on webView:shouldStartLoadWithRequest:navigationType: to only load certain pages. Another way would be check navigationType value:

enum {
   UIWebViewNavigationTypeLinkClicked,
   UIWebViewNavigationTypeFormSubmitted,
   UIWebViewNavigationTypeBackForward,
   UIWebViewNavigationTypeReload,
   UIWebViewNavigationTypeFormResubmitted,
   UIWebViewNavigationTypeOther
};
Marcelo
  • 9,916
  • 3
  • 43
  • 52
  • 2
    when i print out request.URL, i get this: about:blank? Why is that so? – lakshmen Mar 04 '13 at 04:04
  • 3
    I think that's because you're loading a HTML file and not an actual page. – Marcelo Mar 04 '13 at 04:07
  • Isn't it working? That CSS rule should remove the links. The code I provided only prevents the UIWebView to load other requests. – Marcelo Mar 04 '13 at 04:12
  • what u mean? what CSS rule? – lakshmen Mar 04 '13 at 04:17
  • The one inside stringByEvaluatingJavaScriptFromString: [webUI stringByEvaluatingJavaScriptFromString:@"document.styleSheets[0].addRule(\".active\", \"pointer-events: none;\");document.styleSheets[0].addRule(\".active\", \"cursor: default;\")"]; – Marcelo Mar 04 '13 at 04:20
  • You can try yo replace ".active" to "a", so all the links get disabled. Just tested on my browser and it worked. – Marcelo Mar 04 '13 at 04:21
  • it still happens: i typed this: [webUI stringByEvaluatingJavaScriptFromString:@"document.styleSheets[0].addRule(\"a\", \"pointer-events: none;\");document.styleSheets[0].addRule(\"a\", \"cursor: default;\")"]; – lakshmen Mar 04 '13 at 04:35
  • Does your HTML file have any style sheets? If it doesn't, I guess it won't work. This method returns a string; you could print its value to help debugging. – Marcelo Mar 04 '13 at 04:39
  • Maybe you can create one before adding the rules: http://stackoverflow.com/questions/524696/how-to-create-a-style-tag-with-javascript – Marcelo Mar 04 '13 at 04:48
  • Do you have control of the HTML file? If so, can't you just remove the "a" tags? Or create one inline stylesheet so the previous code works? – Marcelo Mar 04 '13 at 04:49
  • I am getting some data from the server. So the data is dynamic -> so just can't remove the "a" tags. How to create one inline stylesheet? – lakshmen Mar 04 '13 at 04:52
  • If you can create it on your HTML, just add a inside the "head" tag. If not, check the link I posted before: http://stackoverflow.com/questions/524696/how-to-create-a-style-tag-with-javascript – Marcelo Mar 04 '13 at 04:53
  • my question is how i add it in using iOS? edited the question to tell u my HTML string. – lakshmen Mar 04 '13 at 04:54