How to check if an app is installed from a web-page on an iPhone?

后端 未结 10 973
忘了有多久
忘了有多久 2020-11-22 09:57

I want to create a web-page, a page that will redirect an iPhone to the app-store if the iPhone does not have the application installed, but if the iPhone has the app instal

10条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 10:14

    After compiling a few answers, I've come up with the following code. What surprised me was that the timer does not get frozen on a PC (Chrome, FF) or Android Chrome - the trigger worked in the background, and the visibility check was the only reliable info.

    var timestamp             = new Date().getTime();
    var timerDelay              = 5000;
    var processingBuffer  = 2000;
    
    var redirect = function(url) {
      //window.location = url;
      log('ts: ' + timestamp + '; redirecting to: ' + url);
    }
    var isPageHidden = function() {
        var browserSpecificProps = {hidden:1, mozHidden:1, msHidden:1, webkitHidden:1};
        for (var p in browserSpecificProps) {
            if(typeof document[p] !== "undefined"){
            return document[p];
          }
        }
        return false; // actually inconclusive, assuming not
    }
    var elapsedMoreTimeThanTimerSet = function(){
        var elapsed = new Date().getTime() - timestamp;
      log('elapsed: ' + elapsed);
      return timerDelay + processingBuffer < elapsed;
    }
    var redirectToFallbackIfBrowserStillActive = function() {
      var elapsedMore = elapsedMoreTimeThanTimerSet();
      log('hidden:' + isPageHidden() +'; time: '+ elapsedMore);
      if (isPageHidden() || elapsedMore) {
        log('not redirecting');
      }else{
        redirect('appStoreUrl');
      }
    }
    var log = function(msg){
        document.getElementById('log').innerHTML += msg + "
    "; } setTimeout(redirectToFallbackIfBrowserStillActive, timerDelay); redirect('nativeApp://');

    JS Fiddle

提交回复
热议问题