检测iOS是否正在使用网络应用程序

Detect if iOS is using webapp

本文关键字:网络 应用程序 iOS 是否 检测      更新时间:2023-09-26

我想知道是否有可能检测到iOS用户正在使用网络应用程序,或者只是使用safari浏览器以正常方式访问。

我想实现的原因是,在iOS网络应用程序上,当用户点击链接时,他会被重定向到Safari浏览器。因此,我使用以下变通方法让他留在网络应用程序中(防止切换到safari浏览器)。

$( document ).on("click",".nav ul li a",
        function( event ){
        // Stop the default behavior of the browser, which
        // is to change the URL of the page.
        event.preventDefault();
        // Manually change the location of the page to stay in
        // "Standalone" mode and change the URL at the same time.
        location.href = $( event.target ).attr( "href" );
        }
    );

但我希望这种变通方法只在用户使用网络应用程序时发生,我希望它对网络应用程序用户是有条件的。所以不是在默认的safari浏览器上。

您必须使用一些javascript:来检测

<script>
if (("standalone" in window.navigator) &&       // Check if "standalone" property exists
    window.navigator.standalone){               // Test if using standalone navigator
    // Web page is loaded via app mode (full-screen mode)
    // (window.navigator.standalone is TRUE if user accesses website via App Mode)
} else {
    // Web page is loaded via standard Safari mode
    // (window.navigator.standalone is FALSE if user accesses website in standard safari)
}
</script>
</head> 

现在需要额外的检查"standalone" in window.navigator,因为有些浏览器没有standalone属性,并且您不希望这些浏览器的代码崩溃。

iOS和Chrome WebApp的行为不同,这就是我得出以下结论的原因:

isInWebAppiOS = (window.navigator.standalone == true);
isInWebAppChrome = (window.matchMedia('(display-mode: standalone)').matches);