2013-08-01 164 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