Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to target Huawei specific user-agents

I have been trying to find a way to target Huawei mobile devices so I can redirect them to the Huawei app store, but can't seem to find a solution. The one I have thought of so far is writing a regex to check for HUAWEI | Huawei | huawei in the string returned from the navigator.userAgent object.. but I am not sure if it will work for all such devices even though that is what i think looking at these sample userAgents

Is there an emulator or something like browserstack to try this out?

like image 236
Kolyo Peev Avatar asked Oct 22 '25 06:10

Kolyo Peev


2 Answers

You can query the useragent to determine whether the current device is a Huawei phone.

Currently, Huawei browser provides UAs based on the HMS or GMS device type. The following are examples (the browser version number, HMS version number, GMS version number, and device information will change accordingly):

  1. Both the HMS and GMS are installed. The following is an example:

Mozilla/5.0 (Linux; Android 6.0.1; LYA-AL00;HMSCore/4.0.0 GMS/10.4 ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.64 HuaweiBrowser/10.0.3.102 Mobile Safari/537.36

  1. If only the HMS is installed, the following is an example:

Mozilla/5.0 (Linux; Android 6.0.1; LYA-AL00;HMSCore/4.0.0 ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.64 HuaweiBrowser/10.0.3.102 Mobile Safari/537.36

  1. Only the GMS is installed. The following is an example:

Mozilla/5.0 (Linux; Android 6.0.1; LYA-AL00;GMS/10.4 ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.64 HuaweiBrowser/10.0.3.102 Mobile Safari/537.36

  1. There is no HMS or GMS. The following is an example:

Mozilla/5.0 (Linux; Android 6.0.1; LYA-AL00 ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.64 HuaweiBrowser/10.0.3.102 Mobile Safari/537.36

like image 189
zhangxaochen Avatar answered Oct 23 '25 19:10

zhangxaochen


I don't know about targeting a specific type of mobile phone, but you can test the user agent string:

/**
 * Determine the mobile operating system.
 * This function returns one of 'iOS', 'Android', 'Windows Phone', or 'unknown'.
 *
 * @returns {String}
 */
function getMobileOperatingSystem() {
  var userAgent = navigator.userAgent || navigator.vendor || window.opera;

      // Windows Phone must come first because its UA also contains "Android"
    if (/windows phone/i.test(userAgent)) {
        return "Windows Phone";
    }

    if (/android/i.test(userAgent)) {
        return "Android";
    }

    // iOS detection from: http://stackoverflow.com/a/9039885/177710
    if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
        return "iOS";
    }

    return "unknown";
}
like image 39
NuggetOfWisdom Avatar answered Oct 23 '25 21:10

NuggetOfWisdom