简体   繁体   English

防止UIWebView为电话链接显示UIPopoverController

[英]Prevent UIWebView from showing UIPopoverController for tel links

I'm working on an iPad application with UIWebView(Deployment target iOS 5.0+). 我正在使用UIWebView(部署目标iOS 5.0+)开发iPad应用程序。

My UIWebView should do nothing when user taps emails, addresses and phone numbers. 当用户点击电子邮件,地址和电话号码时,我的UIWebView应该什么也不做。

I have a problem with phone numbers . 我的电话号码有问题。 If html page contains links like this: <a href="tel:555-555-5555">555-555-5555</a> when user taps this link UIPopoverController appears(Add to Contacts, Copy). 如果html页面包含以下链接: <a href="tel:555-555-5555">555-555-5555</a>当用户点击此链接时,将显示UIPopoverController(添加到联系人,复制)。

I have tried next: 我接下来尝试过:

  • Switch off detection in xib file for UIWebView 关闭xib文件中的UIWebView检测
  • webView.dataDetectorTypes = UIDataDetectorTypeNone;
  • call JS - document.documentElement.style.webkitTouchCallout = "none"; 调用JS- document.documentElement.style.webkitTouchCallout = "none";

UIWebView delegate method shouldStartLoadWithRequest doesn't work for telephone numbers on iPad but works fine on iPhone . UIWebView委托方法shouldStartLoadWithRequest不适用于iPad上的电话号码,但可以在iPhone上正常使用

Do you have any ideas? 你有什么想法?

The reason that particular phone number link isn't getting disabled is that it's not a detected phone number, it's just a regular old <a href=foo> link that happens to have a telephone link for its href attribute. 特定电话号码链接未被禁用的原因是,它不是检测到的电话号码,它只是一个常规的旧<a href=foo>链接,碰巧具有其href属性的电话链接。 Setting the dataDetectorTypes like you're doing is the the right way to disable phone number detection. 像执行dataDetectorTypes一样设置dataDetectorTypes是禁用电话号码检测的正确方法。 So if you have phone numbers just appearing in the web page text somewhere and you want to prevent those from becoming links, keep using webView.dataDetectorTypes = UIDataDetectorTypeNone; 因此,如果您的电话号码仅出现在网页文本中的某个位置,并且希望防止这些电话成为链接,请继续使用webView.dataDetectorTypes = UIDataDetectorTypeNone; .

If you want to disable all links that look like phone numbers you will need to execute a JavaScript after the page has finished loading. 如果要禁用所有看起来像电话号码的链接,则需要在页面加载完成后执行JavaScript。 In your UIWebView's delegate's webViewDidFinishLoad method use [webView stringByEvaluatingJavaScriptFromString:] and pass in JavaScript to replace the offending links. 在您的UIWebView委托的webViewDidFinishLoad方法中,使用[webView stringByEvaluatingJavaScriptFromString:]并传入JavaScript替换有问题的链接。 Here's an example JavaScript that would replace all your tel style links with text elements showing the phone number instead. 这是一个示例JavaScript,它将用显示电话号码的文本元素替换您所有的电话样式链接。 You might want something slightly different, or maybe you just want to remove the links from the DOM entirely. 您可能想要一些稍有不同的东西,或者您只想从DOM中完全删除链接。

var links = document.getElementsByTagName('a');
for (var j = 0; j < links.length; j++) {
    var href=links[j].getAttribute('href');
    var prefix = href.substring(0,4);
    if (prefix == "tel:") {
        var parentNode = links[j].parentNode;
        var replacementNode = document.createTextNode(href.substring(prefix.length, href.length));
        parentNode.replaceChild(replacementNode, links[j]);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM