开发者

Figuring if a link is clicked on a webView - iPhone

I am using a custom webview to show a tabbar when user touches on the webview. But when I clicked on a link in a webview, tabbar is shown for a little time and then page load开发者_如何学Pythons. I am using this custom webview to detect touch: http://github.com/psychs/iphone-samples/blob/b772b963f2acf18d478b41b44555992fcd8d4f31/WebViewTappingHack/Classes/PSWebView.m

Can I detect if a link clicked or not? Because a link stands on the webview, webview detects the touch and also it loads the page... I want to prevent it.

Thanks in advance.


Using Javascript

Instead of preventing the loading of the page, I would inject Javascript into the webview so that touching the link doesn't do anything.

I've answered a question similar to this not too long ago. Instead of disabling all links (like the other question) just look for the specific link you'd like to disable and remove its href attribute.

Using a UIWebview Delegate

Alternatively, if you want to be able to respond to a user attempting to click the link (perhaps to give them a message), you can set the UIWebview's delegate and implement the webView:shouldStartLoadWithRequest:navigationType: method and return NO if the URL attempting to be loaded is the one you'd like to block.

As an aside, it's usually best practice to maintain a whitelist as opposed to a blacklist for this sort of exclusion. Rather than blocking links you don't want it might be better to block all links, except for the ones you know are safe to navigate to.


UIWebViewNavigationTypeLinkClicked = Tells you when a link has clicked. Only check you call

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;
}


OK, I did a workaround and solved by myself...

My links are at the top & bottom of the page so I got screen coordinates by the following code and if pos.y < some value and pos.y > some value then do not show 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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜