I'm trying to understand how can i detect when user opens my site in mobile safari. Chrome and safari have absolutely the same parameters: Chrome "Mozilla/5.0 (iPad; CPU OS 8_1_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12B440 Safari/600.1.4" Safari "Mozilla/5.0 (iPad; CPU OS 8_1_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12B440 Safari/600.1.4" Is there any way to detect only mobile safari?
1 Answers
1
votes
Chrome for developers website states:
The UA in Chrome for iOS is the same as the Mobile Safari user agent, with CriOS/ instead of Version/< VersionNum>.
So the right useragent check will be:
var ua = navigator.userAgent;
if (ua.match(/(iPod|iPhone|iPad)/) !== null && ua.match(/AppleWebKit/) !== null && ua.search('CriOS') < 0) {
alert("You are using Mobile Safari!");
} else {
alert("Whatever this is, it's not Mobile Safari...");
}
Here's the fiddle - https://jsfiddle.net/h8j9n2m5/1/. Checked myself on iPad iOS 7.0.4, Mobile Safari 7.0, works like a charm :)