This is the best I can come up with, but it's only tested in iOS 5:
If your link in mobile safari is <a href="myapp://path">link text</a>
,
change it to: <a href="http://mydomain.com/launchapp?location=path">link text</a>
,
then at the top of the /launchapp
page, put a hidden iframe with the desired URL: <iframe src="myapp://path" style="display:none"></iframe>
. In the body of the page, put your message about needing to go to the appstore to download the app.
If the user has the app:
They will not see the launchapp
page, they will be directed seemlessly to the app url.
If the user does not have the app:
They will get a nasty alert about 'The URL could not be loaded', click OK, then they will be looking at your 'you need to download the app' page.
Update - March 2013
Check out this comment on a related SO answer. Apparently, if your app isn't already installed, you can seamlessly redirect to your app in the App Store using an approach like this (not tested):
// setup fallback (redirect to App Store)
var start = (new Date()).valueOf();
setTimeout(function() {
var end = (new Date()).valueOf();
// prevent App Store redirect upon returning to safari from myapp
if (end - start > 1000) return;
// get a seamless redirect if you use itms://... instead of http://itunes.com/...
window.location = 'itms://my/app/URI';
}, 25);
// Attempt to load my custom url scheme
window.location = 'myapp://my/path'