Determine if user navigated from mobile Safari

后端 未结 12 661
情书的邮戳
情书的邮戳 2020-11-29 19:40

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

相关标签:
12条回答
  • 2020-11-29 20:25

    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.

    0 讨论(0)
  • 2020-11-29 20:27

    Seeing all the answers, here are some tips about the proposed RegExes:

    • AppleWebKit matches Desktop Safari too (not only mobile)
    • no need to call .match more than once for such simple regexes, and prefer the lighter .test method.
    • the g (global) regex flag is useless while the i (case insensitive) can be useful
    • no need for capture (parenthesis), we are just testing the string

    I'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)

    0 讨论(0)
  • 2020-11-29 20:27
    function isIOS {
      var ua = window.navigator.userAgent;
      return /(iPad|iPhone|iPod).*WebKit/.test(ua) && !/(CriOS|OPiOS)/.test(ua);
    }
    
    0 讨论(0)
  • 2020-11-29 20:34

    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.

    0 讨论(0)
  • 2020-11-29 20:35

    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)
    
    0 讨论(0)
  • 2020-11-29 20:35

    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));
    
    0 讨论(0)
提交回复
热议问题