18

The general problem

In Javascript, I need to be notified when there's an error loading a URL and override the default behavior. For example, executing the following on iOS Safari...

window.location = 'http://pageDoesNotExist.badFormatting';

... will pop up this alert message...

mobile safari canno open page

I would like attach a listener for such an error and do something instead of showing an ugly alert box.

My specific problem

When a user taps a button, I need to launch my app if it is installed, else I need to open the App Store for her to download my app. The accepted solution is:

// Attempt to open app
window.location = 'myApp://';

// If app fails to open, will open app store 0.5 seconds later
window.setTimeout(
   function() {
      window.location = 'http://itunes.apple.com/myAppId';
   }, 
   500
);

The problem here is that the first window.location opens the ugly alert box when the app is not installed. The code will then fallback to the App Store. When the user returns from the App Store back to Safari, the ugly alert box is still there. As far as I know window.onerror doesn't fire on mobile Safari.

Community
  • 1
  • 1
JoJo
  • 19,587
  • 34
  • 106
  • 162
  • Take a look at this - http://stackoverflow.com/questions/1108693/is-it-possible-to-register-a-httpdomain-based-url-scheme-for-iphone-apps-like/1109200#1109200 – mrtsherman Jan 06 '12 at 22:19
  • 1
    I think you'll have to have a browser plug-in to do something like this because, somewhat by definition, when this error comes up, there is no active page and thus none of your page javascript. So, the javascript would have to be built into the browser - thus the thought of using a plug-in. – jfriend00 Jan 06 '12 at 22:22
  • Did you take a look at this [solution](http://stackoverflow.com/a/1404662/371392) first? – Web User Jan 08 '12 at 19:16
  • **mrtsherman**: I am already using that solution. **jfriend00**: iOS doesn't allow browser plugins, only Android does. **Web User**: That solution pops up a confirm box. I would like it to be seamless to the user - just go to page1 if it can, else go to page2 without warning the user. – JoJo Jan 08 '12 at 19:29
  • The best solution I can find is using iframe and it completely solved the problems: http://stackoverflow.com/a/24133372/777273 – formatjam Mar 25 '15 at 22:21

2 Answers2

3

Your solution is here.

Basically, you can use cookies to determine wether your application is installed or not.

Community
  • 1
  • 1
ldiqual
  • 15,015
  • 6
  • 52
  • 90
  • They don't. They are only here to determine weither `customurl://` can be used or not. – ldiqual Jan 15 '12 at 11:38
  • How about the first time the user taps the button and the cookie is not there? And how is the cookie in synch with your app installation? When you delete the app, the cookie will be wrong. – JoJo Jan 15 '12 at 22:22
  • If there is no cookie, the user is asked to say weither the app is installed or not. I don't think you can access safari's cookies with an app. And yes, the cookie will be wrong if you remove the app. In other words, you can't avoid an error message try to access `customurl://` while your app not being installed. But it's an elegant way to avoid it. – ldiqual Jan 16 '12 at 14:41
-1

Add an object with the width="0" attribute to the markup, use the page URL as the value of the data attribute, then go to iTunes if the URL redirects to a 404:

/* Find object element */
var hrefobj = document.querySelector("object");

/* Assign protocol string to data attribute of object element */
hrefobj.data = 'myApp://'; 

/* If the assignment fails */
if (hrefobj.data !== 'myApp://')
  {
  /* Redirect the window to the fallback URL */
  window.location = 'http://itunes.apple.com/myAppId';
  }
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265