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 loads. I am using this custom webview to detect touch: http://github.com/psychs/iphone-samples/blob/b772b963f2acf18d478b41b44555992fcd8d4f31/WebViewTappingHack/Classes/PSWebView.m
我使用自定义的webview在用户触摸webview时显示一个tabbar。但是当我点击webview中的一个链接时,tabbar会显示一段时间,然后页面加载。我正在使用这个自定义webview来检测触摸:http://github.com/psychs/iphone- samples/blob/b772b963f2f2acf18d478b41b41b445992fcd8d4fcd3f31 / 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.
我能检测到一个链接是否被点击了吗?因为一个链接站在webview上,webview会检测触摸并加载页面……我想阻止它。
Thanks in advance.
提前谢谢。
3 个解决方案
#1
3
Using Javascript
使用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.
我不会阻止页面的加载,而是将Javascript注入到webview中,这样触摸链接就什么都不做了。
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.
不久前,我回答了一个类似的问题。与其禁用所有链接(比如其他问题),不如查找您希望禁用并删除其href属性的特定链接。
Using a UIWebview Delegate
使用UIWebview委托
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.
或者,如果您希望能够响应用户试图点击链接(可能给他们一个消息),您可以设置UIWebview的委托实施webView:shouldStartLoadWithRequest:navigationType:方法并返回没有如果URL试图加载你想阻止。
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.
顺便说一句,通常最好的做法是保留一个白名单,而不是将其列入黑名单。与其屏蔽你不想要的链接,不如屏蔽所有链接,除了那些你知道可以安全导航的链接。
#2
2
UIWebViewNavigationTypeLinkClicked = Tells you when a link has clicked. Only check you call
uiwebviewnavigationtypelinkclick =告诉你一个链接何时被点击。只是检查你叫
self.webView.delegate = self;
self.webView.delegate =自我;
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[[UIApplication sharedApplication] openURL:request.URL];
return NO;
}
return YES;
}
#3
0
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...
我的链接在页面的顶部和底部,所以我通过以下代码获得了屏幕坐标,如果pos.y < some value和pos.y >有些值,那么就不显示菜单……
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...
希望这可以帮助别人……
#1
3
Using Javascript
使用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.
我不会阻止页面的加载,而是将Javascript注入到webview中,这样触摸链接就什么都不做了。
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.
不久前,我回答了一个类似的问题。与其禁用所有链接(比如其他问题),不如查找您希望禁用并删除其href属性的特定链接。
Using a UIWebview Delegate
使用UIWebview委托
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.
或者,如果您希望能够响应用户试图点击链接(可能给他们一个消息),您可以设置UIWebview的委托实施webView:shouldStartLoadWithRequest:navigationType:方法并返回没有如果URL试图加载你想阻止。
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.
顺便说一句,通常最好的做法是保留一个白名单,而不是将其列入黑名单。与其屏蔽你不想要的链接,不如屏蔽所有链接,除了那些你知道可以安全导航的链接。
#2
2
UIWebViewNavigationTypeLinkClicked = Tells you when a link has clicked. Only check you call
uiwebviewnavigationtypelinkclick =告诉你一个链接何时被点击。只是检查你叫
self.webView.delegate = self;
self.webView.delegate =自我;
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[[UIApplication sharedApplication] openURL:request.URL];
return NO;
}
return YES;
}
#3
0
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...
我的链接在页面的顶部和底部,所以我通过以下代码获得了屏幕坐标,如果pos.y < some value和pos.y >有些值,那么就不显示菜单……
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...
希望这可以帮助别人……