Find out if there is a link on a web browser - iPhone

I use custom webview to show a tab when the user touches the webview. But when I clicked the link in the web view, the tab appears for a bit of time, and then the page loads. I use this custom webview to detect touch: http://github.com/psychs/iphone-samples/blob/b772b963f2acf18d478b41b44555992fcd8d4f31/WebViewTappingHack/Classes/PSWebView.m

Can I tell if a link has been clicked or not? Since the link is on the web view, the web view detects a touch, and also it loads the page ... I want to prevent it.

Thanks in advance.

+3
source share
3 answers

Ok, I made a workaround and decided myself ...

My links are at the top and bottom of the page, so I got the screen coordinates using the following code, and if pos.y <some value and pos.y> some value, then do not display the menu ...

UITouch * touch = [touches anyObject];
CGPoint pos = [touch locationInView: [UIApplication sharedApplication].keyWindow];
NSLog(@"Position of touch: %.3f, %.3f", pos.x, pos.y);

Hope this helps someone ...

0
source

Using javascript

Instead of preventing the page from loading, I would inject Javascript into the webview so that touching the link does nothing.

I answered a question like this not so long ago. Instead of disabling all links (for example, another question), simply find the specific link you want to disable and remove its attributehref .

Using the UIWebview Delegate

, , (, ), UIWebview webView:shouldStartLoadWithRequest:navigationType: NO, URL-, , - , .

, , . , , , , , .

+3

UIWebViewNavigationTypeLinkClicked = , . ,

self.webView.delegate = self;

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        [[UIApplication sharedApplication] openURL:request.URL];
        return NO;
    }
    return YES;
}
+2

Source: https://habr.com/ru/post/1736911/


All Articles