iPad not counting as "Mobile" for WebGL check.

I have a WebGL app that uses a JSLIB file that checks if my app is running on mobile to change the control scheme from keyboard/mouse to touch. Everything seems to work properly except for iPads, which gets detected as non-mobile for some reason.

Here’s the JSLIB file I’m using, which I got from this forum thread: How to detect if a mobile is running the WebGL scene

mergeInto(LibraryManager.library,
{
  IsMobileBrowser: function ()
  {
    return (/iPhone|iPad|iPod|Android/i.test(navigator.userAgent));
  }
});

It has iPad in there. Am I doing something wrong?

That code ransacks the browser’s useragent string… I’m thinking perhaps that doesn’t have “ipad” in it?

I just googled and it told me this giant blurb… it does NOT seem to have iPad

I think maybe the code should look for that webkit stuff??? Unsure.

That useragent string will change over time of course, and varies by which browser they use.

OR… maybe iPad Pros are trying to sound like they’re more of a “real desktop” so they don’t send that??

2 Likes

It seems iPads are a tricky beast. This (found here) worked for me (tested on my iPad):

.jslib file (in Assets/Plugins/WebGL)

var kamgamDeviceDetector = {

    KamgamIsMobilePlatform : function()
    {
        var userAgent = navigator.userAgent;
        isMobile = (
                    /\b(BlackBerry|webOS|iPhone|IEMobile)\b/i.test(userAgent) ||
                    /\b(Android|Windows Phone|iPad|iPod)\b/i.test(userAgent) ||
                    // iPad on iOS 13 detection
                    (userAgent.includes("Mac") && "ontouchend" in document)
                );
        return isMobile;
    }
 
};

mergeInto(LibraryManager.library, kamgamDeviceDetector);

Unity c# Code:

using System.Runtime.InteropServices;
using UnityEngine;

namespace Kamgam
{
    public static class MobileDeviceDetector
    {
        [DllImport("__Internal")]
        public static extern bool KamgamIsMobilePlatform();

        public static bool IsMobileBrowser()
        {
#if UNITY_EDITOR
            return false; // value to return in Play Mode (in the editor)
#elif UNITY_WEBGL
            return KamgamIsMobilePlatform(); // value based on the current browser
#else
            return Application.isMobilePlatform;
#endif
        }
    }
}

I am not sure if it reports false positives on MACs with touch display. AFAIK there are no official Apple touch monitors or laptops. Yet, if anyone can test that please let me know the results.

1 Like