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应用程序所需的内容将不会重定向到同一页面。