2013-08-01 44 views
12

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

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

     } 
    ); 

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

回答

23

你必须使用一些JavaScript来检测这样的:

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

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

由于可以在这里找到:Determine if site is open as web app or through regular safari on ipad?

而在答案中使用的源THIS

+2

尼斯和简单的答案!我搜索了c的stackoverflow,但由于某种原因该主题没有弹出。即使当我输入我的问题的标题时......谢谢! – koningdavid

+1

我会的,只需要再等3分钟;) – koningdavid

+0

@koningdavid:很高兴帮助你! –

4

的iOS和Chrome Web应用程序的行为不同,这就是我来到了以下理由:

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

请小心'window.matchMedia('(display-mode:standalone)')。matches':在桌面Chrome弹出窗口中也是'true'。 – Antelle