[关闭]
@njy 2017-01-03T01:46:45.000000Z 字数 4431 阅读 1821

h5页面上唤起小炮APP

新浪彩通


唤起Android

  1. function adjustBase() {
  2. var clientWidth = util.viewData().viewWidth;
  3. var clientHeight = util.viewData().viewHeight;
  4. if (clientWidth > 500) {
  5. clientWidth = 500;
  6. }
  7. $("body").width(clientWidth);
  8. $("body").height(clientHeight);
  9. }
  10. adjustBase();
  11. window.onresize = function(){
  12. adjustBase();
  13. }
  14. // 微信遮罩提示
  15. if (window.navigator.userAgent.toLowerCase().indexOf('micromessenger') >= 0) {
  16. window.$wxbox = $("#ct0_wx_box");
  17. if($wxbox.length>0){
  18. $wxbox.show();
  19. $wxbox.on("click",function(){
  20. $wxbox.hide();
  21. })
  22. }
  23. }
  24. window.popup = {
  25. show: function($layout) {
  26. var $mask = $("#ct0_mask_full");
  27. var $popup_box = $(".ct0_popup");
  28. var viewData = util.viewData();
  29. var layout = $layout[0];
  30. document.body.style.overflow = 'hidden';
  31. $popup_box.hide();
  32. $layout.show();
  33. layout.style.visibility = "hidden";
  34. var cHeight = layout.offsetHeight;
  35. layout.style.marginTop = (viewData.viewHeight / 2 - cHeight / 2 - 60) + 'px';
  36. $mask.show();
  37. layout.style.visibility = "visible";
  38. },
  39. maskShow: function() {
  40. document.body.style.overflow = 'hidden';
  41. var $mask = $("#ct0_mask_full");
  42. $mask.show();
  43. },
  44. hide: function() {
  45. var $mask = $("#ct0_mask_full");
  46. var $popup_box = $(".ct0_popup");
  47. document.body.style.overflow = 'auto';
  48. $mask.hide();
  49. $popup_box.hide();
  50. },
  51. alert: function(msg, callback) {
  52. var self = this;
  53. var $popup_alert = $("#popup_alert");
  54. $("#popup_alert_txt").html(msg);
  55. popup.show($popup_alert);
  56. callback && callback();
  57. }
  58. }
  59. window.h5OpenApp = function(){
  60. // android下载地址
  61. // var aphoneDLink = "http://wap.133.cn/download/FlightManager-BMoaweb.apk";
  62. var aphoneDLink = "http://n.sinaimg.cn/sports/lottpao/v120/app/caitong_v1.0_caitong_release.apk";
  63. // scheme
  64. var schemeDLink = "lotterylaunch://com.sina.lottery.gai/open";
  65. // ios下载地址
  66. // var iphoneDLink = "https://itunes.apple.com/cn/app/zhi-fu-bao-rang-sheng-huo/id333206289?mt=8&v0=WWW-GCCN-ITSTOP100-FREEAPPS&l=&ign-mpt=uo%3D4";
  67. // 应用宝下载地址
  68. // var imttDLink = "http://imtt.dd.qq.com/16891/9033AE4E61B3A7ECB6F36298AC651976.apk?fsname=com.flightmanager.view_6.2_117.apk&csr=4d5s";
  69. var browser = {
  70. versions: function() {
  71. var u = navigator.userAgent;
  72. return {
  73. mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
  74. ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
  75. android: u.indexOf('Android') > -1 || u.indexOf('Adr') > -1, //android终端
  76. weixin: u.indexOf('MicroMessenger') > -1, //是否微信 (2015-01-22新增)
  77. qq: u.match(/\sQQ/i) == " qq", //是否QQ
  78. trident: u.indexOf('Trident') > -1, //IE内核
  79. presto: u.indexOf('Presto') > -1, //opera内核
  80. webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
  81. gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1,//火狐内核
  82. iPhone: u.indexOf('iPhone') > -1 , //是否为iPhone或者QQHD浏览器
  83. iPad: u.indexOf('iPad') > -1, //是否iPad
  84. webApp: u.indexOf('Safari') == -1 //是否web应该程序,没有头部与底部
  85. };
  86. }()
  87. }
  88. window.bv = browser.versions;
  89. function openApp(openUrl, appUrl) {
  90. //检查app是否打开
  91. function checkOpen() {
  92. var _clickTime = +(new Date());
  93. function check(elsTime) {
  94. if (elsTime > 3000 || document.hidden || document.webkitHidden) {
  95. // alert("调用app成功");
  96. } else {
  97. window.location.href = appUrl;
  98. }
  99. }
  100. //启动间隔20ms运行的定时器,并检测累计消耗时间是否超过3000ms,超过则结束
  101. var _count = 0,
  102. intHandle;
  103. intHandle = setInterval(function() {
  104. _count++;
  105. var elsTime = +(new Date()) - _clickTime;
  106. if (_count >= 100 || elsTime > 3000) {
  107. clearInterval(intHandle);
  108. check(elsTime);
  109. }
  110. }, 20);
  111. }
  112. //在iframe 中打开APP
  113. var ifr = document.createElement('iframe');
  114. ifr.src = openUrl;
  115. ifr.style.display = 'none';
  116. checkOpen();
  117. document.body.appendChild(ifr);
  118. setTimeout(function() {
  119. document.body.removeChild(ifr);
  120. }, 2000);
  121. }
  122. $("#xiaopaoDownload").on('click', function(e) {
  123. // e.preventDefault();
  124. if(bv.weixin || bv.qq){
  125. if(window.$wxbox){
  126. $wxbox.show();
  127. } else {
  128. window.$wxbox = $("#ct0_wx_box");
  129. if($wxbox.length>0){
  130. $wxbox.show();
  131. $wxbox.on("click",function(){
  132. $wxbox.hide();
  133. })
  134. }
  135. }
  136. // 腾讯应用宝页面
  137. // var msg = '<div class="popup_p1">请在浏览器打开页面下载</div>';
  138. // popup.alert(msg);
  139. // if(bv.ios || bv.iPhone || bv.iPad){
  140. // window.location.href = iphoneDLink;
  141. // } else {
  142. // window.location.href = imttDLink;
  143. // }
  144. } else if(bv.ios || bv.iPhone || bv.iPad){
  145. //ios
  146. // window.location.href = iphoneDLink;
  147. // setTimeout(function() {
  148. // location.href = 'http://lotto.sina.cn/ai/app/download.d.html';
  149. // }, 250);
  150. // setTimeout(function() {
  151. // location.reload();
  152. // }, 1000);
  153. var msg = '<div class="popup_p1">请期待ios版小炮APP</div>';
  154. popup.alert(msg);
  155. } else if(bv.android || bv.mobile){
  156. //android
  157. openApp(schemeDLink,aphoneDLink);
  158. } else {
  159. var msg = '<div class="popup_p1">请使用手机访问下载</div>';
  160. popup.alert(msg);
  161. // location.href = 'http://lotto.sina.cn/ai/app/download.d.html';
  162. }
  163. })
  164. var $mask = $("#ct0_mask_full");
  165. $mask.length>0 && $mask.bind("touchmove", function(e) {
  166. e.preventDefault();
  167. return false;
  168. });
  169. $("#popup_alert").on("click", function(){
  170. popup.hide();
  171. return false;
  172. });
  173. }
  174. window.h5OpenApp();

另外,可以通过 document.hidden 或 document.[webkit|moz|ms]Hidden 来判断页面是否被置入后台(即应用被唤起),或visibilitychange事件,但对于Android 4.4版本一下则不支持。

提示iOS

参考资料:
iOS/Android 浏览器(h5)及微信中唤起本地APP

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注