I have an app, and I\'d like to redirect the users to different pages based on where they are navigating from.
If navigating from web clip, do not redirect. If navig
I upvoted @unwitting 's answer, as it inevitably got me going. However, when rendering my SPA in an iOS Webview, I needed to tweak it a bit.
function is_iOS () {
/*
Returns (true/false) whether device agent is iOS Safari
*/
var ua = navigator.userAgent;
var iOS = !!ua.match(/iPad/i) || !!ua.match(/iPhone/i);
var webkitUa = !!ua.match(/WebKit/i);
return typeof webkitUa !== 'undefined' && iOS && webkitUa && !ua.match(/CriOS/i);
};
The main difference being, the renaming of webkit
to webkitUa
, so as to prevent clashing with the root webkit
object used as a message handler between the SPA & UIView.
Seeing all the answers, here are some tips about the proposed RegExes:
AppleWebKit
matches Desktop Safari too (not only mobile).match
more than once for such simple regexes, and prefer the lighter .test
method.g
(global) regex flag is useless while the i
(case insensitive) can be usefulI'm just using this since getting true
for mobile Chrome is OK for me (same behavior):
/iPhone|iPad|iPod/i.test(navigator.userAgent)
(I just want to detect if the device is a target for an iOS app)
function isIOS {
var ua = window.navigator.userAgent;
return /(iPad|iPhone|iPod).*WebKit/.test(ua) && !/(CriOS|OPiOS)/.test(ua);
}
I was looking for this answer and I remembered I came across this before.
The most reliable way to detect Safari on iOS in JavaScript is
if (window.outerWidth === 0) {
// Code for Safari on iOS
}
or
if (window.outerHeight === 0) {
// Code for Safari on iOS
}
For some reason Safari on iOS returns 0 for window.outerHeight property and window.outerWidth property.
This is for all iPads and iPhones on all versions of iOS. Every other browser and device this property works normally.
Not sure if they intend to change this but for now it works well.
Falling code only find mobile safari and nothing else (except dolphin and other small browsers)
(/(iPad|iPhone|iPod)/gi).test(userAgent) &&
!(/CriOS/).test(userAgent) &&
!(/FxiOS/).test(userAgent) &&
!(/OPiOS/).test(userAgent) &&
!(/mercury/).test(userAgent)
Merged all the answers and comments. And this is the result.
function iOSSafari(userAgent)
{
return /iP(ad|od|hone)/i.test(userAgent) && /WebKit/i.test(userAgent) && !(/(CriOS|FxiOS|OPiOS|mercury)/i.test(userAgent));
}
var iOSSafari = /iP(ad|od|hone)/i.test(window.navigator.userAgent) && /WebKit/i.test(window.navigator.userAgent) && !(/(CriOS|FxiOS|OPiOS|mercury)/i.test(window.navigator.userAgent));