最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Safari throwing alert when checking if an app SchemeURL is valid - Stack Overflow

programmeradmin1浏览0评论

I am using the following code to check if a mobile app exists on the users phone, if it does then open app, if not then redirect to app store. It works clean on Android, but on Safari simply just checking if the Scheme/URL exists pops up a Safari cannot open the page because the address is invalid

  function checkReadyState() {
    if (reader.readyState === 4) {
      //check to see whether request for the file failed or succeeded
      if ((reader.status == 200) || (reader.status == 0)) {
        //page exists -- redirect to the url
        console.log(mobile+ " : does exist") ;
        document.location.href = mobile ;
      } else {
        //if the url does not exist
        console.log(mobile+ " : does NOT exist") ;
        goToStore() ;
      }
    }
    
  }// end of checkReadyState()
     
  console.log("Redirect to App or to Store") ;
  var mobile = "myapp://path/to/resource" ; 
  var reader = new XMLHttpRequest();
  reader.open('get', mobile, true);
                
  //check each time the ready state changes
  //to see if the object is ready
  reader.onreadystatechange = checkReadyState;
    
  // Sends the request for the file data to the server
  // Use null for "get" mode
  reader.send(null);    

I added all this because simply doing a window.location = mobile triggered the same Alert message. I thought this would do things behind the scene and not alert the user interrupting the UI flow.

How can I fix this or what other method can I use to simply check if an app/scheme exists without triggering an alert on the users phone?

I am using the following code to check if a mobile app exists on the users phone, if it does then open app, if not then redirect to app store. It works clean on Android, but on Safari simply just checking if the Scheme/URL exists pops up a Safari cannot open the page because the address is invalid

  function checkReadyState() {
    if (reader.readyState === 4) {
      //check to see whether request for the file failed or succeeded
      if ((reader.status == 200) || (reader.status == 0)) {
        //page exists -- redirect to the url
        console.log(mobile+ " : does exist") ;
        document.location.href = mobile ;
      } else {
        //if the url does not exist
        console.log(mobile+ " : does NOT exist") ;
        goToStore() ;
      }
    }
    
  }// end of checkReadyState()
     
  console.log("Redirect to App or to Store") ;
  var mobile = "myapp://path/to/resource" ; 
  var reader = new XMLHttpRequest();
  reader.open('get', mobile, true);
                
  //check each time the ready state changes
  //to see if the object is ready
  reader.onreadystatechange = checkReadyState;
    
  // Sends the request for the file data to the server
  // Use null for "get" mode
  reader.send(null);    

I added all this because simply doing a window.location = mobile triggered the same Alert message. I thought this would do things behind the scene and not alert the user interrupting the UI flow.

How can I fix this or what other method can I use to simply check if an app/scheme exists without triggering an alert on the users phone?

Share Improve this question asked Mar 13 at 15:13 rolingerrolinger 3,1061 gold badge38 silver badges69 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I resolved my own issue, posting the result:

  if ((reader.status == 200) || (reader.status == 0)) {

the reader.status == 0 was causing the issue. If the url scheme doesn't exist on the users phone the reader status was returning a 0, which allowed it into IF statement to execute the document.location.href - and it was the href that was causing the alert.

Its now:

 if (reader.status == 200) {
发布评论

评论列表(0)

  1. 暂无评论