Check if URL scheme is supported in javascript

2019-01-02 14:40发布

Is there any way to check if a URL scheme is currently registered on the phone... with javascript?

10条回答
十年一品温如言
2楼-- · 2019-01-02 15:04

This is based on the answer of mrahman. As noted, by JoshNaro new Date() gives back a wrong date when called inside the timeout. Tests suggest that the date is not updated in threads that are started before the app is deactivated.

A further ugly setTimeout called after activation will create a new thread with the current date.

This was tested on iOS 8.

function startiThrown() {
    document.location = appurl;
    var time = (new Date()).getTime();
    setTimeout(function(){
        setTimeout(function(){ // <-- start new thread after activation
            var now = (new Date()).getTime();
            if((now - time)<400) {
                if(confirm('You do not seem to have iThrown installed, do you want to go download it now?')){
                    document.location = 'http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=293049283&mt=8&uo=6';
                }
            }
        }, 10); // <-- start new thread after activation
    }, 300);
}
查看更多
柔情千种
3楼-- · 2019-01-02 15:06

I try to use just the 'pagehide' event, but then it not work into Firefox. I created this version here http://jsfiddle.net/thiagomata/6tvoc4f1/2/ what works in Firefox, Google Chrome and Safari. I have not tested in Internet Explorer yet.

One thing what was necessary to make it work into Firefox, was use Iframe to set the src. This allows me to call the app without leaving my page.

<a class="uri-link" href="#" 
  data-uri-app="myapp://" 
  data-url-app-not-found="http://www.google.com?q=not-found-link"
  >
  Example 1
</a>​
<a class="uri-link" href="#" 
  data-uri-app="myapp://" 
  data-url-app-not-found="http://www.google.com?q=not-found-link"
  data-url-app-found="http://www.google.com?q=found-link"
  >
  Example 2
</a>​
<a class="uri-link"  href="#"
  data-uri-app="notexists://" 
  data-url-app-not-found="http://www.google.com?q=not-exists"
>
  Example 3
</a>​
<iframe id="callapp" style="display:none"></iframe>
查看更多
荒废的爱情
4楼-- · 2019-01-02 15:09

I found pagehide event to be more robust than depending on system time. For those of us who prefers a non-jQuery favor, here is the snippet.

  var appurl = 'custom://url';
  var appstore = 'https://itunes.apple.com/us/app/your-app';

  var timeout;
  function preventPopup() {
    clearTimeout(timeout);
    timeout = null;
    window.removeEventListener('pagehide', preventPopup);
  }
  function startApp() {
    window.location = appurl;
    timeout = setTimeout(function(){
      if(confirm('You do not seem to have the App installed, do you want to go download it now?')){
        document.location = appstore;
      }
    }, 1000);
    window.addEventListener('pagehide', preventPopup);
  }
查看更多
看淡一切
5楼-- · 2019-01-02 15:10

No, not from a webpage.

查看更多
像晚风撩人
6楼-- · 2019-01-02 15:12

Another great (at least working in latest browser versions) workaround is to check if the browser window has focus after a short timeout, this way you can show a dialog box to the user only if the URI scheme didn't work

HTML:

<a class="uri-link" data-uri="qobuzapp://" href="#">URI</a>​

Javascript (using jQuery here):

var windowHasFocus;

$(window).focus(function() {
  windowHasFocus = true;
}).blur(function() {
  windowHasFocus = false;
});

function goToUri(uri) {
  window.location = uri;
  setTimeout(function(){
    if (windowHasFocus) {
      if (confirm('You do not seem to have Qobuz installed, do you want to go download it now?')){
        window.location = 'http://www.qobuz.com';
      }
    }
  }, 100);
}

$('a').on('click', function(){ 
  goToUri($(this).data('uri')); 
});​

Here is a working jsFiddle, just update it with your own URI scheme: http://jsfiddle.net/mF6TZ/

查看更多
心情的温度
7楼-- · 2019-01-02 15:13

Not seamlessly. But there is a way similar to checking if a pop-up was blocked or not.

When you try a URL scheme which is not supported, Safari will warn the user that it doesn't know what to do with it and stay on the same page.

So if you gave your app-call some time to activate, say 300 ms, and then do something else to respond to the non-existence of the scheme.

It's not the prettiest but it works:

function startIThrown(){
  document.location = 'ithrown://restart';
  setTimeout(function(){
    if(confirm('You do not seem to have iThrown installed, do you want to go download it now?')){
      document.location = 'http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=293049283&mt=8&uo=6';
    }
  }, 300);
}

<a href="#" onclick="startIThrown()">Restart iThrown</a>
查看更多
登录 后发表回答