Skip to content

Instantly share code, notes, and snippets.

@zengfenfei
Last active August 4, 2021 15:18
Show Gist options
  • Save zengfenfei/51b36c008faaea4cb934 to your computer and use it in GitHub Desktop.
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.
/*
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;
@ashokrkm
Copy link

This stopped working in iOS9, has something changed or is there a fix to make it work with iOS 9? thank you!

@barsh
Copy link

barsh commented Jul 23, 2015

I'm keen on a fix for iOS9 too!

@luigisaggese
Copy link

iOS 9 fails 👎

@zengfenfei
Copy link
Author

It does fail on iOS 9. I fail to find a solution yet.

@djleonskennedy
Copy link

Doesn't work on IOS 9, i love f**n apple

@rolinger
Copy link

rolinger commented Aug 4, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment