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?
Are you sure that the only way for the user to navigate from the page you’re loading is by touching
Aelements? What about form submit buttons or JavaScript event handlers that setwindow.location?As far as I know, the bulletproof way to prevent navigation is to implement
webView: shouldStartLoadWithRequest: navigationTypeto returnNOexcept on the first request:UPDATE
UIWebViewhas been deprecated for a while now in favor ofWKWebView. If you are usingWKWebView, you’ll want to set itsnavigationDelegate, and in the delegate, implement thewebView(_:decidePolicyFor:preferences:decisionHandler:)method.