Detect if device is iOS

前端 未结 17 1603
一整个雨季
一整个雨季 2020-11-22 01:39

I\'m wondering if it\'s possible to detect whether a browser is running on iOS, similar to how you can feature detect with Modernizr (although this is obviously device detec

17条回答
  •  温柔的废话
    2020-11-22 02:26

    If you are using Modernizr, you can add a custom test for it.

    It doesn't matter which detection mode you decide to use (userAgent, navigator.vendor or navigator.platform), you can always wrap it up for a easier use later.

    //Add Modernizr test
    Modernizr.addTest('isios', function() {
        return navigator.userAgent.match(/(iPad|iPhone|iPod)/g);
    });
    
    //usage
    if (Modernizr.isios) {
        //this adds ios class to body
        Modernizr.prefixed('ios');
    } else {
        //this adds notios class to body
        Modernizr.prefixed('notios');
    }
    

提交回复
热议问题