1. 程式人生 > >Javascript detect mobile device and iOS Version « 關於網路那些事...

Javascript detect mobile device and iOS Version « 關於網路那些事...

Best way

Use mobile-detect.js to check iOS, device... is recommended.

But, if you wanna a easy way to detect mobile and iOS, you can start with :

Detect Mobile Device

var isMobile = {
    Android: function() {
        return navigator.userAgent.match(/Android/i);
    },
    BlackBerry: function() {
return navigator.userAgent.match(/BlackBerry/i); }, iOS: function() { return navigator.userAgent.match(/iPhone|iPad|iPod/i); }, Opera: function() { return navigator.userAgent.match(/Opera Mini/i); }, Windows: function() { return navigator.userAgent.
match(/IEMobile/i); }, any: function() { return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows()); } };

Detect iOS Version

/* 
  [ Detect iOS Version ]
  supports iOS 2.0 or latter
  call iOSversion() function will get:
    status: boolean;  -- get iOS version: true, none: false
    version: int; -- ex 11
    info: string; -- ex IOS 11.2.6 
*/
function iOSversion() { let d, v; if (/iP(hone|od|ad)/.test(navigator.platform)) { v = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/); d = { status: true, version: parseInt(v[1], 10) , info: parseInt(v[1], 10)+'.'+parseInt(v[2], 10)+'.'+parseInt(v[3] || 0, 10) }; }else{ d = {status:false, version: false, info:''} } return d; }

Examples:

let iosVer = iOSversion();
if (iosVer.status) {
  alert('iOS : '+iosVer.info+', version : '+iosVer.version);
  //ex. IOS 11.2.6, version 11

}else{
  console.log('not iPhone or iPad');
}

如果你喜歡我們的文章內容,請在這裡按個讚