0

I am trying to get a function to be called when a phone number is tapped on my UIWebView but this seems a little tricky.
The link is pretty straightforward, it looks like <a href="tel:23231343">23231343</a>

So All I need to do is simply emit an ajax to googleAnalytics so I can tell a phone call was placed. (I know it might not be actually placed, if the user declines, but I'd like to get the ajax nonetheless)

I was expecting to receive a call in the callback: webView:shouldStartLoadWithRequest:navigationType, but it doesn't fire.

Gruntcakes
  • 37,738
  • 44
  • 184
  • 378
Ted
  • 3,805
  • 14
  • 56
  • 98

1 Answers1

0

Inject this script into the view:

(function(){
    window.addEventListener("click", function(event) {
        var element = event.target;
        do{
            if(element.href && element.href.indexOf("tel:") === 0){
                var request = new XMLHttpRequest();
                request.open("GET", "https://www.example.org");
                request.send();
            }
        }while(element = element.parentNode);
    });
})();
metarmask
  • 1,747
  • 1
  • 16
  • 20
  • I am not sure, should this function be placed in document.ready? and, does this work for you? I tried using something similar in document.ready and it still did not fire – Ted May 17 '16 at 19:38
  • Realized what I did was very ineffective and won't work if elements are added later. I updated my answer to use event bubbling instead. You don't have to wait for anything before you add the updated script. – metarmask May 17 '16 at 20:07