https://stackoverflow.com/questions/10237031/how-to-open-a-native-ios-app-from-a-web-app var frame = document.createElement('iframe'); frame.src = 'myapp://?params=...'; frame.style.display = 'none'; document.body.appendChild(frame); setTimeout(function() { document.body.removeChild(frame); }, 4); The above plan is arousing app When the call fails, there will be no default behavior of the browser,If used href The jump will have the pop-up prompt behavior of the browser
var preventDefault = function(e) { e = e || window.event; if(e.preventDefault) { e.preventDefault(); }else{ e.returnValue = false; } } preventDefault(); The above method has been tried, and the pop-up behavior of browser on mobile phone can be prohibited
https://segmentfault.com/a/1190000011231042
I haven't tried the following methods
Ios/Android h5 evokes local APP
LAN Ming Xiang Issued on September 19, 2017
- Like 4 collection 26
- 2.3k views
After two days of entanglement (local APP is called up in browser), no solution has been found, and it is basically settled today.
ps: Tucao about the magic window article, why not directly open the js code open source, confusing code to see me annoyed.
Reference article: Magic window solution,JD solutions
The first is to judge the browser
// Judge browser var Navigator = navigator.userAgent; var ifChrome = Navigator.match(/Chrome/i) != null && Navigator.match(/Version\/\d+\.\d+(\.\d+)?\sChrome\//i) == null ? true : false; var ifAndroid = (Navigator.match(/(Android);?[\s\/]+([\d.]+)?/)) ? true : false; var ifiPad = (Navigator.match(/(iPad).*OS\s([\d_]+)/)) ? true : false; var ifiPhone = (!ifiPad && Navigator.match(/(iPhone\sOS)\s([\d_]+)/)) ? true : false; var ifIos = Navigator.match(/iPhone|iPad|iPd/i) ? true : false; var ifSafari = ifIos && Navigator.match(/Safari/); // Version number of ios device var iosVersion = Navigator.match(/OS\s*(\d+)/) iosVersion = iosVersion ? (iosVersion[1] || 0) : 0; // Android version number var androidVersion = Navigator.match(/Android\s*(\d+)/) androidVersion = androidVersion ? (androidVersion[1] || 0) : 0;
Android 5 and above
// Delay 50 ms setTimeout(function() { location.href = 'custom URL' }, 50)
IOS 9 and above
setTimeout(function() { // Must use settimeout var a = document.createElement("a"); //Create a element a.setAttribute("href", 'custom URL'), a.style.display = "none", document.body.appendChild(a); var t = document.createEvent("HTMLEvents"); // Returns the newly created Event object with the specified type. t.initEvent("click", !1, !1) // Initialize the properties of the new event object a.dispatchEvent(t) // Binding events }, 0)
iframe in all cases
document.querySelector("#" + iframe).src = 'custom URL' // Add src to iframe
Open APP for time difference calculation scheme
var checkOpen = function (cb){ var _clickTime = +(new Date()); function check(elsTime) { if ( elsTime > 3000 || document.hidden || document.webkitHidden) { cb(1); } else { cb(0); } } //Start the timer with an interval of 20ms, and check whether the accumulated consumption time is more than 3000ms, otherwise it will be over var _count = 0, intHandle; intHandle = setInterval(function(){ _count++; var elsTime = +(new Date()) - _clickTime; if (_count>=100 || elsTime > 3000 ) { clearInterval(intHandle); check(elsTime); } }, 20); } checkOpen(function(opened){ // APP is not opened successfully and will jump to download page automatically if(opened === 0 && option.autoRedirectToDownloadUrl){ location.href = downloadUrl; } });