Do you know if there is a way to detect which device is being used? For example, detect if it is iOS5, iOS6, iOS6s, iOS6s plus, etc. And on android if it's Samsung, Hawei, LG, etc. Thank you very much
Do you know if there is a way to detect which device is being used? For example, detect if it is iOS5, iOS6, iOS6s, iOS6s plus, etc. And on android if it's Samsung, Hawei, LG, etc. Thank you very much
/**
* Determina el sistema operativo del móvil.
* Esta función retorna 'iOS', 'Android', 'Windows Phone', or 'desconocido'.
*
* @returns {String}
*/
function getMobileOperatingSystem() {
var userAgent = navigator.userAgent || navigator.vendor || window.opera;
// Windows Phone debe ir primero porque su UA tambien contiene "Android"
if (/windows phone/i.test(userAgent)) {
return "Windows Phone";
}
if (/android/i.test(userAgent)) {
return "Android";
}
if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
return "iOS";
}
return "desconocido";
}
iOS detection from: link
Credits to: link
Yes, the best way is to use the User Agent provided by the browser headers. Just keep in mind that this result can be changed through "plugins" and you could be showing the wrong site. Another thing is that the amount of User Agent values is immense: List of possible User Agent results . Yes, there are many who are in disfavor but it is equal something to keep in mind while you are designing the experiences between results.
Other solutions include in addition to the "navigator.useragent" the "navigator.platform" or javascript libraries that do the same ... salu2!
You can do it manually, detecting the user agent
or platform
or you can not reinvent the wheel and use a library to be more productive. For example, the library isMobile .
Through this library you can know if it is an Appe (phone, ipod, tablet) or Android (phone and tablet) device. For example to know if it is a mobile (cell phone):
if (isMobile.apple.phone) {
// hacer algo
}
if (isMobile.android.phone) {
// hacer otra cosa
}
Or if you are not interested in which device it is, just be it iOS or Android:
if (isMobile.apple.device) {
// hacer algo
}
if (isMobile.android.device) {
// hacer otra cosa
}
With this library you can detect more OS, such as Windows or Amazon devices or 7-inch devices.
Note: library works for both sides, client and server. If used in Node.js, the isMobile
header must be passed to the user-agent
function.