Determine if user navigated from mobile Safari

后端 未结 12 660
情书的邮戳
情书的邮戳 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:11

    See https://developer.chrome.com/multidevice/user-agent#chrome_for_ios_user_agent - the user agent strings for Safari on iOS and for Chrome on iOS are inconveniently similar:

    Chrome

    Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_1_1 like Mac OS X; en) AppleWebKit/534.46.0 (KHTML, like Gecko) CriOS/19.0.1084.60 Mobile/9B206 Safari/7534.48.3

    Safari

    Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543 Safari/419.3

    Looks like the best approach here is to first of all check for iOS as other answers have suggested and then filter on the stuff that makes the Safari UA unique, which I would suggest is best accomplished with "is AppleWebKit and is not CriOS":

    var ua = window.navigator.userAgent;
    var iOS = !!ua.match(/iPad/i) || !!ua.match(/iPhone/i);
    var webkit = !!ua.match(/WebKit/i);
    var iOSSafari = iOS && webkit && !ua.match(/CriOS/i);
    
    0 讨论(0)
  • 2020-11-29 20:13

    this regex works for me, clean and simple

    const isIOSSafari = !!window.navigator.userAgent.match(/Version/[\d.]+.*Safari/);

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

    best practice is:

    function isMobileSafari() {
        return navigator.userAgent.match(/(iPod|iPhone|iPad)/) && navigator.userAgent.match(/AppleWebKit/)
    }
    
    0 讨论(0)
  • 2020-11-29 20:22

    I know this is an old thread, but I'd like to share my solution with you guys.

    I needed to detect when an user navigates from Desktop Safari (Because we're in middle 2017, and Apple hasn't give any support for input[type="date"] YET...

    So, I made a fallback custom datepicker for it) . But only applies to safari in desktop because that input type works fine in mobile Safari. So, I made this Regex to only detect desktop Safari. I already tested it and it doesn't match with Opera, Chrome, Firefox or Safari Mobile.

    Hope it may help some of you guys.

    if(userAgent.match(/^(?!.*chrome).(?!.*mobile).(?!.*firefox).(?!.*iPad).(?!.*iPhone).*safari.*$/i)){
      $('input[type="date"]').each(function(){
        $(this).BitmallDatePicker();
      })
    }
    
    0 讨论(0)
  • 2020-11-29 20:23

    Actually, there isn't a silver bullet of detecting mobile safari. There are quite a few browsers may use the keywords of the user agent of mobile safari. Maybe you can try feature detection and keep updating the rule.

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

    UPDATE: This is a very old answer and I cannot delete it because the answer is accepted. Check unwitting's answer below for a better solution.


    You should be able to check for the "iPad" or "iPhone" substring in the user agent string:

    var userAgent = window.navigator.userAgent;
    
    if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i)) {
       // iPad or iPhone
    }
    else {
       // Anything else
    }
    
    0 讨论(0)
提交回复
热议问题