0

我正在一個網站上工作。將iOS webApp用戶重定向到Safari?

iPhone用戶有能力「保存到家庭屏幕」我的網站。這樣做會在iPhone的主屏幕上放置一個圖標,以便用戶可以作爲WebApp(全屏模式,Safari視圖)訪問我的網站。

我正嘗試將webApp用戶重定向到正常的Safari。我不希望用戶在WebApp模式下查看我的網站。

這是代碼:

window.onload=function(){ 
    if (navigator.standalone) { 
     console.log ('Running iOS webApp, lets send user to Safari'); 
     window.location.href = "http://www.urlToWebsite.com"; 
    } else { 
     console.log ('Running in a normal browser'); 
    } 
}; 

從每個人都在網上的帖子,鏈接在iOS的Web應用程序「模式」打開重定向到Safari瀏覽器。

然而,在這種情況下,我點擊了使用javascript的鏈接,重定向到Safari並沒有發生。相反,webApp會重新加載。

如何成功重定向到Safari?

+1

你需要一個真正的用戶點擊,沒有愚蠢的iOS ... – dandavis

回答

2

如果用戶點擊某個鏈接,您只能從Web應用重定向回Safari,您只需觸發鏈接點擊即可。

以下是您將重定向回Safari的基本頁面的代碼。

<!DOCTYPE html> 
<html> 
<head> 
    <meta name="apple-mobile-web-app-capable" content="yes"> 
    <meta name="apple-mobile-web-app-status-bar-style" content="black"> 
    <title>iOS Web App Test</title> 
</head> 
<body> 
<script> 
    if (window.navigator.standalone == true) { 
     document.write('You are viewing this website as an iOS web App'); 
     document.write('<a href="http://www.urlToWebsite.com/?a" id="redirect-link" style="display: none;">Safari Website</a>'); 
     document.getElementById("redirect-link").click(); 
    } else { 
     document.write('You are viewing this website in Safari.'); 
    } 
</script> 
</body> 
</html> 

您會注意到「?a」被添加到重定向鏈接的末尾,因爲iOS Web應用程序所需的內容將不會重定向到同一頁面。