简体   繁体   English

检测 iOS 是否正在使用 webapp

[英]Detect if iOS is using webapp

I was wondering if it's possible to detect if an iOS user is using the webapp, or just visiting the normal way with safari browser.我想知道是否可以检测 iOS 用户是否正在使用 webapp,或者只是使用 safari 浏览器以正常方式访问。

The reason I want to achieve is that on a iOS webapp when a user click on a link, he will get redirected to the Safari browser.我想要实现的原因是,在 iOS 网络应用程序上,当用户单击链接时,他将被重定向到 Safari 浏览器。 So I'm using the following workaround to make him stay in the webapp(prevent the switching to safari browser).所以我使用以下解决方法让他留在 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" );

        }
    );

But I want this workaround only to happen when the user is using the webapp, I want it to be conditional for webapp users.但是我希望这种解决方法只在用户使用 webapp 时发生,我希望它对 webapp 用户有条件。 So not on the default safari browser.所以不是在默认的 safari 浏览器上。

You have to detect this by using some javascript:您必须使用一些 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> 

Now the extra check "standalone" in window.navigator is needed because some browsers do not have the standalone property and you don't want your code to crash for those browsers.现在需要"standalone" in window.navigator额外检查"standalone" in window.navigator因为有些浏览器没有standalone属性,你不希望你的代码在这些浏览器上崩溃。

iOS and Chrome WebApp behaves different, thats the reason i came to following: iOS 和 Chrome WebApp 的行为不同,这就是我开始关注的原因:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM