检测iOS是否正在使用webapp

2024-01-30

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

我想要实现的原因是,在 iOS Web 应用程序上,当用户单击链接时,他将被重定向到 Safari 浏览器。所以我使用以下解决方法让他留在 web 应用程序中(防止切换到 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" );

        }
    );

但我希望这种解决方法仅在用户使用 web 应用程序时发生,我希望它对 web 应用程序用户是有条件的。所以不在默认的 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属性,并且您不希望您的代码在这些浏览器中崩溃。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

检测iOS是否正在使用webapp 的相关文章

随机推荐