Home  >  Article  >  Web Front-end  >  JS implements clicking on the web page to determine whether to install the app and open it, otherwise jump to the app store

JS implements clicking on the web page to determine whether to install the app and open it, otherwise jump to the app store

高洛峰
高洛峰Original
2016-12-06 14:12:071937browse

There are often scenarios where the APP we develop needs to be promoted, such as a large banner picture or a QR code at the top of the page. But often we directly add a download link (from the App Store) to the promotional image. So let’s simulate the user’s operation steps:

1. The user visits the promotion page for the first time

a. Click Banner and enter the corresponding APP download page in the APP Store

b. The APP download page prompts: Install; The user clicks to install

c. After the installation is completed, the APP download page prompts: Open; the user continues to click to open

d. The user uses the APP normally

2. The user visits the promotion page for the second time

a. Click Banner to enter Go to the corresponding APP download page in the APP Store

b. The APP download page prompts: Open; the user directly clicks to open

c. The user uses the APP normally

3. The user’s third, fourth,..., For the Nth visit, the operation steps are the same as 2

It can be seen that whether it is clicking the Banner or scanning the QR code, the experience is very bad for users who have already installed the APP.

A better experience is: after clicking the Banner (or scanning the QR code), the program will determine whether the App has been installed on the current system. If not, it will automatically jump to the App Store download page; otherwise, open the App directly.

On iOS, to add a large Banner for an APP, you only need to add a e8e496c15ba93d81f6ea4fe5f55a2244 tag within the 93f0f5c25f18dab9d176bd4f6de5d30e tag. The format is as follows:

<meta name=&#39;apple-itunes-app&#39; content=&#39;app-id=你的APP-ID&#39;>

For example, add a Baidu Tieba Native APP Banner, use the following code:

<meta name=&#39;apple-itunes-app&#39; content=&#39;app-id=477927812&#39;>

As for whether it can be opened directly after clicking the link, you can use the following code to achieve it. Prerequisite: You must know the opening protocol corresponding to your APP, such as Tieba APP, the protocol is: com.baidu.tieba://, WeChat: weixin://, and so on. . .

<!-- a标签的链接,设置为对应的下载链接;点击打开的动作,在click事件中注册 -->
<a href="https://itunes.apple.com/cn/app/id477927812" id="openApp">贴吧客户端</a>
<script type="text/javascript">
document.getElementById(&#39;openApp&#39;).onclick = function(e){
// 通过iframe的方式试图打开APP,如果能正常打开,会直接切换到APP,并自动阻止a标签的默认行为
// 否则打开a标签的href链接
var ifr = document.createElement(&#39;iframe&#39;);
ifr.src = &#39;com.baidu.tieba://&#39;;
ifr.style.display = &#39;none&#39;;
document.body.appendChild(ifr);
window.setTimeout(function(){
document.body.removeChild(ifr);
},3000)
};
</script>

Of course, if you design it as a QR code, you can use the following code:

<!-- a标签的链接,设置为对应的下载链接;点击打开的动作,在click事件中注册 -->
<a href="https://itunes.apple.com/cn/app/id477927812" id="openApp" style="display: none">贴吧客户端</a>
<script type="text/javascript">
document.getElementById(&#39;openApp&#39;).onclick = function(e){
// 通过iframe的方式试图打开APP,如果能正常打开,会直接切换到APP,并自动阻止a标签的默认行为
// 否则打开a标签的href链接
var ifr = document.createElement(&#39;iframe&#39;);
ifr.src = &#39;com.baidu.tieba://&#39;;
ifr.style.display = &#39;none&#39;;
document.body.appendChild(ifr);
window.setTimeout(function(){
document.body.removeChild(ifr);
},3000)
};
document.getElementById(&#39;openApp&#39;).click();

Which one to use depends on your actual scenario!

When we browse the web, you will see a prompt box "Open APP" or "Download APP" floating below the web page. If your phone has already installed this APP, then the web page will prompt "Open APP" ”, if it is not installed, it will prompt “download APP”. How is this implemented from a technical perspective? Let me share this technology with you. When the company was working on a project for the International Cartoon and Animation Festival last year, the customer mentioned this requirement. When clicking on the web page company, open the APP directly (if it is already installed). If it has not been installed, open it directly. APP page

Now I will share the source code of this piece

if(navigator.userAgent.match(/android/i)) {
// 通过iframe的方式试图打开APP,如果能正常打开,会直接切换到APP,并自动阻止a标签的默认行为
// 否则打开a标签的href链接
var isInstalled;
//下面是安卓端APP接口调用的地址,自己根据情况去修改
var ifrSrc = &#39;cartooncomicsshowtwo://platformapi/startApp? type=0&id=${com.id}&phone_num=${com.phone_num}&#39;;
var ifr = document.createElement(&#39;iframe&#39;);
ifr.src = ifrSrc;
ifr.style.display = &#39;none&#39;;
ifr.onload = function() {
// alert(&#39;Is installed.&#39;);
isInstalled = true;
alert(isInstalled);
document.getElementById(&#39;openApp0&#39;).click();};
ifr.onerror = function() {
// alert(&#39;May be not installed.&#39;);
isInstalled = false;
alert(isInstalled);
}
document.body.appendChild(ifr);
setTimeout(function() {
document.body.removeChild(ifr);
},1000);
}
//ios判断
if(navigator.userAgent.match(/(iPhone|iPod|iPad);?/i))
if(navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) {
//Animation://com.yz.animation
var isInstalled;
//var gz = &#39;{"comName":"${com.short_name}","comID":"${com.id}","comPhoneNum":"${com.phone_num}","type":"0"}&#39;;
//var jsongz =JSON.parse(gz);
//下面是IOS调用的地址,自己根据情况去修改
var ifrSrc = &#39;Animation://?comName=${com.short_name}&comID=${com.id}&comPhoneNum=${com.phone_num}&type=0&#39;;var ifr = document.createElement(&#39;iframe&#39;);
ifr.src = ifrSrc;
ifr.style.display = &#39;none&#39;;
ifr.onload = function() {
// alert(&#39;Is installed.&#39;);
isInstalled = true;
alert(isInstalled);
document.getElementById(&#39;openApp1&#39;).click();};
ifr.onerror = function() {
// alert(&#39;May be not installed.&#39;);
isInstalled = false;
alert(isInstalled);
}
document.body.appendChild(ifr);
setTimeout(function() {
document.body.removeChild(ifr);
},1000);
}
}

Everyone needs to pay attention to two issues during the process:

1. The interface address must be written correctly. You can check the schema protocol , called through this protocol

2. When using Android, if you use WeChat scan or QQ browser to scan the code

There will be a problem when using the above protocol, which is that you must use APK to list it on Tencent Go to the app market


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn