简体   繁体   English

iOS Web应用程序 - 仅在添加到主屏幕时显示内容?

[英]iOS Web app - show content only if added to home screen?

I've previously seen a 'solution' to this, but with a major flaw! 我以前见过这个“解决方案”,但有一个重大缺陷!

Here's what I'm trying to achieve; 这就是我想要实现的目标;

I have a web-app that requires a user to login before it can be used. 我有一个网络应用程序,要求用户登录才能使用它。 For a variety of reasons, I don't want it to be used in regular Safari, only via the Home Screen (aesthetically, it only really 'works' when in full screen). 出于各种原因,我不希望它在常规Safari中使用,只能通过主屏幕(从美学角度来说,它只在全屏时才“正常”)。

So, when a user browses to the site, it should detect whether it's been opened via the home screen (in which case it shows the login page) or regular safari (in which case it shows a splash screen inviting the viewer to add it to their home screen). 因此,当用户浏览网站时,它应检测是否已通过主屏幕打开(在这种情况下显示登录页面)或常规的Safari(在这种情况下,它会显示一个启动画面,邀请查看者将其添加到他们的主屏幕)。

I can successfully detect whether it's opened via the Home Screen or now (using window.navigator.standalone), however all the solutions I've come across involve redirecting the user to a different page if it's not opened via the home screen. 我可以成功检测它是通过主屏幕打开还是现在(使用window.navigator.standalone),但是我遇到的所有解决方案都涉及将用户重定向到不同的页面,如果它没有通过主屏幕打开。 The problem with this is that the user would be bookmarking (or adding to home screen) the wrong page. 这个问题是用户会将错误的页面加入书签(或添加到主屏幕)。 As far as I'm aware, there's no way to specify a different page to be added to the home screen. 据我所知,没有办法指定一个不同的页面添加到主屏幕。

I've tried the following, which doesn't seem to work; 我尝试了以下,似乎没有用;

/* Added to login page head */
$(document).ready()
        if (window.navigator.userAgent.indexOf('iPhone') != -1) {
            if (window.navigator.standalone == true) {
                initialize();
            }else{
                $('.container').load('/install.cfm')
            }
        }else{
            $('.container').load('/install.cfm')
        }

Edit : Based on @scunliffe's comment, I have now tried the following, which also doesn't work (jQuery is loaded before the script executes, so that shouldn't be the problem); 编辑:根据@ scunliffe的评论,我现在尝试了以下,这也行不通(jQuery是在脚本执行之前加载的,所以这应该不是问题);

if (window.navigator.userAgent.indexOf('iPhone') != -1) {
            if (window.navigator.standalone == true) {
                $('#logindiv').show;
            }else{
                $('#installdiv').show;
            }
        }else{
            $('#installdiv').show;
        }

Can you alter your logic so that the login page is the default but if the user is on an iphone/iOS device? 你可以改变你的逻辑,以便登录页面是默认的,但如果用户在iphone / iOS设备上? then they get a message if not running standalone? 如果没有独立运行,他们会得到一条消息?

$(document).ready(function(){
    if(navigator.userAgent.indexOf('iPhone') != -1){//test for other iOS devices?
        if(window.navigator.standalone == true){
            //do stuff!
            initialize();
        } else {
            //show message telling user to add to their home screen...
        }
    } else {
        //show message that this must be run on device X/Y/Z...
    }
});

Update: 更新:

Based on your updated sample, you just need to modify the code to call show/hide as a method. 根据您更新的示例,您只需修改代码即可将show / hide称为方法。

$(document).ready(function(){
  if(window.navigator.userAgent.indexOf('iPhone') != -1){
    if(window.navigator.standalone == true){
      $('#logindiv').show();
    } else {
        $('#installdiv').show();
    }
  } else {
    $('#installdiv').show();
  }
});

and

<div id="logindiv">
  ...stuff to login here...
</div>
<div id="installdiv">
  ...note to install here...
</div>

You could try the hash-trick used in "Add to Home Screen" here is the example here 你可以尝试hash-trick中使用“添加到主屏幕”这里的例子在这里

Basically it checks to see if there is a hash at the end of the URL, if it does then that means it was added to the home screen if it doesn't then it can run a script then add the hash to the url. 基本上它检查URL的末尾是否有哈希值,如果确实如此,那就意味着它被添加到主屏幕,如果没有,那么它可以运行脚本然后将哈希添加到URL。 So when they add the link to the home screen and the next time they open the web app it would have the hash on the screen. 因此,当他们将链接添加到主屏幕时,下次他们打开Web应用程序时,它将在屏幕上显示哈希值。 Its kind of a messy way of doing it but it works. 它是一种混乱的方式,但它的工作原理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有什么方法可以检测用户是否已将Web应用程序添加到其主屏幕? - Is there any way to detect if a user has added a web app to their home screen? 卸载后,Web App不显示“添加到主屏幕” - Web App doesn't show “add to home screen” after uninstall 从iOS 7主屏幕启动时,Web应用程序JS未运行 - Web app JS not running when launched from iOS 7 home screen 如何在 iOS11 主屏幕 Web 应用程序上访问相机? - How to access camera on iOS11 home screen web app? window.open()在添加到主屏幕的移动Safari Web应用程序中不起作用 - window.open() doesn't work in mobile Safari web-app added to home screen 谷歌网络应用程序添加到主屏幕 - google web app add to home screen 如何将Web应用添加到用户主屏幕? - How to add web app to user home screen? iOS:“已添加到主屏幕”网站不会刷新 - iOS: “Added to home screen” website won't refresh 无论如何,是否可以使用Javascript将添加到主屏幕 - 应用程序图标添加到主屏幕? - Is there anyway to find whether the add to home screen - app icon has been added to home screen using Javascript? 如何使用navigator.share()在iOS上显示“添加到主屏幕”? - How to show 'Add to home screen' on iOS with navigator.share()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM