网络编程
位置:首页>> 网络编程>> JavaScript>> iOS和Android用同一个二维码实现跳转下载链接的方法

iOS和Android用同一个二维码实现跳转下载链接的方法

作者:daisy  发布时间:2024-04-22 13:24:16 

标签:ios,Android,二维码,合并

前言

最近一个项目需要iOS和安卓使用一个二维码,让扫描的机器自己识别操作系统实现跳转到相应的下载链接。比如iPhone用微信进行扫描就让他跳转appStore的下载页面,安卓机器使用微信扫描就直接跳浏览器下载。但是这二维码还有一个需求就是,用户已经下载了这个app,当用户打开app进入到注册页面时,再次扫描这个二维码时,自动填写邀请码进行注册。那么该如何实现,细节就不说了,直接上代码。

使用js实现,其实代码非常简单.

使用时直接拷贝代码,改掉相应的链接就好。

PS:该链接在微信环境打开时还是需要手动跳转到手机的浏览器才能跳到下载页面,因为微信内的webView比较特别,所以写了一个alert引导用户打开浏览器。


<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title></title>
</head>
<body>

<script>

/**
   出来的链接大概是长这样的
   http://xxxx.cn/243423.html?c=Q23DR32
 */

// c=Q23DR32是注册时扫描获取的邀请码。
 // 这样加参数,后面的参数会被自动忽略,不会影响加载此网页

goDownload();

// 去下载
   function goDownload() {
     var u = navigator.userAgent, app = navigator.appVersion;
     var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1;
     var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
     // 是安卓浏览器
     if (isAndroid) {
       window.location.href = 'http://xxxxxxx.cn/release/xxxx-release.apk'; // 跳安卓端下载地址
     }
     // 是iOS浏览器
     if (isIOS) {
       window.location.href = 'https://itunes.apple.com/cn/app/xxxxxxx/id1124348115?mt=8'; // 跳AppStore下载地址
     }

// 是微信内部webView
     if (is_weixn()) {
       alert("请点击右上角按钮, 点击使用浏览器打开");
     }

// 是PC端
     if (IsPC()) {
       window.location.href = 'http://www.xxxxxxx.cn/index.html'; // 公司主页
     }
   }

// 是微信浏览器
   function is_weixn(){
     var ua = navigator.userAgent.toLowerCase();
     if(ua.match(/MicroMessenger/i)=="micromessenger") {
       return true;
     } else {
       return false;
     }
   }

function IsPC() {
     var userAgentInfo = navigator.userAgent;
     var Agents = ["Android", "iPhone",
       "SymbianOS", "Windows Phone",
       "iPad", "iPod"];
     var flag = true;
     for (var v = 0; v < Agents.length; v++) {
       if (userAgentInfo.indexOf(Agents[v]) > 0) {
         flag = false;
         break;
       }
     }
     return flag;
   }

</script>
</body>
</html>

总结

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com