Paul Morris
Paul Morris

Reputation: 1773

Removing links in UIWebView with Javascript?

I have a UIWebView being created programatically as follows;

- (void)loadView {
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;
webNews.dataDetectorTypes = UIDataDetectorTypeNone;
[self.view addSubview: webNews];
[webNews loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://..........MYURL......./"]]];

}

I also have the following delegate methods;

I am attempting to remove all links within the UIWebView so my user cannot navigate away from the URL that I have coded in. I have tried the following to change color of links;

- (void)webViewDidFinishLoad:(UIWebView *)webView {
[webNews stringByEvaluatingJavaScriptFromString:@"document.styleSheets[0].addRule(\"a.link\", \"color:#FFFFFF\")"];
}

I have read examples of people using javascript to remove all links from UIWebView but I can't seem to get this working. Can anyone provide an example within the method of what I need to do?

Upvotes: 0

Views: 710

Answers (2)

rob mayoff
rob mayoff

Reputation: 385670

Are you sure that the only way for the user to navigate from the page you're loading is by touching A elements? What about form submit buttons or JavaScript event handlers that set window.location?

As far as I know, the bulletproof way to prevent navigation is to implement webView: shouldStartLoadWithRequest: navigationType to return NO except on the first request:

@interface MyDelegate <UIWebViewDelegate>
{
    BOOL didStartFirstRequest;
}
@end

@implementation MyDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if (didStartFirstRequest)
        return NO;
    didStartFirstRequest = YES;
    return YES;
}
@end

UPDATE

UIWebView has been deprecated for a while now in favor of WKWebView. If you are using WKWebView, you'll want to set its navigationDelegate, and in the delegate, implement the webView(_:decidePolicyFor:preferences:decisionHandler:) method.

Upvotes: 1

Brian Nickel
Brian Nickel

Reputation: 27550

What you're doing here is making all the links with the "link" class look like normal text, not making them unclickable. You could make the links do nothing with the following code:

Array.prototype.forEach.call(document.querySelectorAll('a:link'), function(link) {link.href="javascript://"});

Of course, they'll still look clickable. You could actually remove them with something more like this:

Array.prototype.forEach.call(document.querySelectorAll('a:link'), function(link) {
    while(link.firstChild && link.parentElement) {
        link.parentElement.insertBefore(link.firstChild, link);
        link.parentElement.removeChild(link);
    }
});

But this would have all sorts of other side effects. It really depends on what you're trying to implement.

UPDATE switched from parentNode to parentElement and added check to skip parentless links.

Upvotes: 1

Related Questions