Last active
August 4, 2021 15:18
-
-
Save zengfenfei/51b36c008faaea4cb934 to your computer and use it in GitHub Desktop.
Open native app from webpage, mobile safari "invalid address" warnings be suppressed when the app is not installed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Open native app through iframe so that mobile safari "invalid address" warnings be suppressed when the app is not installed. | |
* `window.location=url` will not open native app in the iframe of android chrome | |
* Android will go to the next page even if the url scheme is not supported | |
*/ | |
function openApp (url) { | |
if (window.chrome && navigator.userAgent.search(/\bChrome\b/)!=-1) { | |
window.location = url; | |
} else { | |
openAppByFrame(url); | |
} | |
} | |
/* | |
# Problems | |
* `window.open` will open tab selection view of iOS Safari | |
*/ | |
function openAppByFrame(url) { | |
var frm = getOpenAppProxyFrame(); | |
var src = '\<script\>window.location="' + url + '";\</script\>'; | |
frm.contentDocument.write(src); | |
} | |
var _openAppProxyFrame; | |
function getOpenAppProxyFrame() { | |
if (_openAppProxyFrame) { | |
return _openAppProxyFrame; | |
} | |
_openAppProxyFrame = document.createElement('iframe'); | |
document.body.appendChild(_openAppProxyFrame); | |
_openAppProxyFrame.style.display = 'none'; | |
return _openAppProxyFrame; | |
} | |
exports.openApp = openApp; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Apple did this on purpose to force Universal Links. Safari...and Apple to a large part....is the new IE.
That being said, the uri scheme still works....I improved upon the best known JS technique to manage uri scheme deep links (similar to above):
https://stackoverflow.com/a/68653745/2036221