检测iOS是否使用webapp [英] Detect if iOS is using webapp

查看:381
本文介绍了检测iOS是否使用webapp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我想要实现的原因是,在iOS webapp上,当用户点击一个链接时,他将被重定向到Safari浏览器。因此,我正在使用以下解决方法,让他留在webapp中(防止切换到Safari浏览器)。

  $(文件).on(click,。nav ul li a,
function(event){

//停止浏览器的默认行为,其中
/ /是更改页面的URL
event.preventDefault();

//手动更改页面的位置以保留
//独立模式并在同一时间更改URL。
location.href = $(event.target).attr(href);

}
);

但是我想要这种解决方法仅在用户使用webapp时发生,我希望它是有条件的webapp用户。所以不要在默认的safari浏览器中使用。

解决方案

b

 < script> 
if(
(window.navigator中的standalone)&&& //检查是否存在standalone属性
!window.navigator.standalone //测试是否使用独立导航器
){

// ....此处代码....
}
< / script>
< / head>

可以在这里找到:确定网站是作为网络应用程序打开还是通过ipad上的常规Safari浏览器?

答案中使用的来源是这是


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.

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. So I'm using the following workaround to make him stay in the webapp(prevent the switching to safari browser).

$( 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. So not on the default safari browser.

解决方案

You have to detect this by using some javascript:

<script>
if (
    ("standalone" in window.navigator) &&       // Check if "standalone" property exists
    !window.navigator.standalone                // Test if using standalone navigator
    ){

    // .... code here ....
}
</script>
</head> 

As can be found here: Determine if site is open as web app or through regular safari on ipad?

And the source used in the answer is THIS.

这篇关于检测iOS是否使用webapp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆