javascriptioswificaptivenetwork

Javascript detection of iOS Captive Network Assistant webpage


I am running a webapp that is displayed just after a user successfully logged on a wifi network using a captive portal.

On iOS, after the user logs in, my webapp is displayed in the CNA (Captive Network Assistant) popup and the top right button label is turned to "Ok" to allow the user to close this popup.

I want to have a specific behavior in my webapp when it is displayed inside this CNA popup, so I am trying to detect (with Javascript) if my webapp is displayed in such a popup.

I first bet on the window.innerHeight value but on my iPhone 5 it seems difficult :

1px difference is, in my point of view, not enough to figure out if I am in this CNA popup.

Is there any other javascript information I can rely on to determine if I am in such a popup ?

Thank you


Solution

  • Well finally I detect the CNA with the user agent. When inside the CNA the user agent does not include "Safari/" in the UA string. Also tested with a dozen of alternative browsers like Opera mini, Dolphin, Mercury, Puffin, Atomic, 360 Lite, ...

    For example Safari UA string is :

    Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25
    

    On the same device, inside a CNA, the user agent string would be :

    Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d
    

    So in PHP my detection looks like :

    $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
    
    if ((strpos($userAgent, 'iphone') || strpos($userAgent, 'ipad')) &&
        (strpos($userAgent, 'mozilla/') !== false) &&
        (strpos($userAgent, 'applewebkit/') !== false) &&
        (strpos($userAgent, 'mobile/') !== false) &&
        (strpos($userAgent, 'safari') === false))
    {
        // Yes, we are in a CNA popup
        [...]
    }