I have a mobile application that sends URL links between the users. The receiving user should click the link so the mobile device open the browser with the link and than starts my application passing the link to it.
I manage this by sending a link to a webpage, that has a redirect to a URL with my application protocol.
An example of such link would be: ://www.myurl.com?somedata
The problem is that if the receiving user did not install my application, he gets an error dialog.
My java-script detects this issue and redirect the user to the download page, but I want to avoid the ugly error dialog, when the page first opens.
Any ideas?
If you need to know, in advance (before actually redirecting to a hyperlink), if a mobile device has an application that supports a protocol, write the following java-script code in the webpage your link was targeting:
var url="myAppName://www.myapp.com";
var request=new XMLHttpRequest();
if(request) {
request.open("GET", url);
if (request.status < 400) {
try{
window.location.replace(url);
}
catch(e){
console.log(e);
}
}
else {
console.log('Failed to find application that supports '+url+' switching to manual download');
window.location.replace("download.application.url");
}
Some explanation:
The general idea is to send the target user a link to a webpage. That webpage (containing the above java-script) makes an ajax call to see if the special protocol URL can be open. If the ajax call fails, the browser does not display an error dialog. Instead, it quietly returns an error status code. The above code, checks if the Ajax's call status code is less than 400, to make sure any good respond means that the mobile application is installed.
If all is ok, the browser opens the mobile application, passing the URL as a parameter and the mobile application process the request.
If the mobile application is not installed, the browser gets an error status code (again no dialog). The browser will redirect the user to a download page of the application.
I try to make things more clear with an example
1. Say we have a mobile application name: mobileapp .
2. Say that user wants to send a text message with some action to be perform by the mobile application on the target device. For simplicity the command to the target user is: dothis .
3. Lets assume that the application's website is www.mobileapp.com .
The link in the message should be like: *http://www.mobileapp.com?cmd=dothis*
When the link is pressed on the target mobile device, the webpage with the above java-script is loaded. If the application is installed on the target mobile device, the browser will start it, passing the *myAppProtocol://www.myapp.com* to it (you need to replace this URL with your own).
If the application is not installed, the browser redirect to the application's download page (Replace download.application.url with your own.