Skip to content

Instantly share code, notes, and snippets.

@vasilisvg
Created May 2, 2012 13:20
Show Gist options
  • Save vasilisvg/2576485 to your computer and use it in GitHub Desktop.
Save vasilisvg/2576485 to your computer and use it in GitHub Desktop.
Check if the page is viewed in the Safari browser in iOs
/*
I want to make a bubble that explains people that they can save the page to their homescreen
I want to be absolutely sure that this is Safari on an iPhone or iPad
Not a browser in a webview in a native app
Not a browser in standalone mode
Not something else
This is the closest I can get to something I kinda trust.
*/
var nav = window.navigator;
var ua = nav.userAgent;
function isiOsSafari (a) {
return ("standalone" in nav) // There's a thing called standalone in nav
&& !nav.standalone // It is not running in standalone mode
&& ua.indexOf(a)!=-1 // iPhone is in the UA string (could be Opera)
&& ua.indexOf('Mac OS')!=-1 // There's Mac in the UA string (not Opera)
&& ua.indexOf('Safari')!=-1
/* if all the above are true this probably means this is
the Safari browser,
not a webview in an app,
not a page in standalone mode */
}
// Check if Mobile Safari on iPhone
if(isiOsSafari('iPhone')){
document.write('Probably Safari on an iPhone: ' + ua);
}
// Check if Mobile Safari on iPod
else if(isiOsSafari('iPad')){
document.write('Probably Safari on an iPad: ' + ua);
}
else{
document.write('Probably something else: ' + ua)
}
@rommyarb
Copy link

rommyarb commented Sep 3, 2018

This still detect safari on Chrome ios. Any solution?

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