简体   繁体   English

iPhone Safari的“添加到主屏幕”的url可以自定义吗?

[英]Can the url for the "Add to home screen" on iPhone Safari be customized?

The "add to home screen" shows up on all pages of a site, and I want the URL to be the homepage that gets saved. “添加到主屏幕”显示在网站的所有页面上,我希望 URL 成为被保存的主页。

For example on this page: http://www.domain.com/category/page.html例如在这个页面上: http://www.domain.com/category/page.html

Is it possible for the "Add to home screen" to save this url: http://www.domain.com “添加到主屏幕”是否可以保存此 url: http://www.domain.com

Any help would be greatly appreciated.任何帮助将不胜感激。

I found a sort-of workaround to this.我找到了一种解决方法。 You can detect that you were launched from the home page via window.navigator.standalone and based upon that potentially redirect.您可以通过window.navigator.standalone检测到您是从主页启动的,并且基于该潜在重定向。

Also, I have done a little testing and found that on the latest iOS, different user agents are reported to the server, which opens the possibility of a faster redirect.另外,我做了一些测试,发现在最新的 iOS 上,不同的用户代理会报告给服务器,这打开了更快重定向的可能性。 I can't find any information about whether this has always been the case.我找不到任何关于这种情况是否一直如此的信息。

Launch from home page:从主页启动:

Mozilla/5.0 (iPhone; CPU iPhone OS 6_0_1 like Mac OS X) 
AppleWebKit/536.26 (KHTML, like Gecko) Mobile/10A523

Mobile Safari:移动Safari:

Mozilla/5.0 (iPhone; CPU iPhone OS 6_0_1 like Mac OS X) 
AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A523 Safari/8536.25

If your page gets most of its content via AJAX or you notice the different user-agent on the server it might be possible to skip the redirect and just act "as if" you were at another URL, since in standalone mode the URL is invisible anyway.如果您的页面通过 AJAX 获取其大部分内容,或者您​​注意到服务器上的不同用户代理,则可能会跳过重定向并“就好像”您在另一个 URL 上一样,因为在独立模式下,该 URL 是不可见的反正。 I'm investigating this but haven't got far enough to say whether it will burn you or not.我正在调查这件事,但还没有说清楚它是否会烧伤你。

Also note that the user's choice of URL to mark as an app may be meaningful, but I'll leave that to your own UX judgment.另请注意,用户选择标记为应用程序的 URL 可能是有意义的,但我将把它留给您自己的 UX 判断。

A combination of both WrightsCS and svachalek Answers - You can't add to home screen a remote page. WrightsCS 和 svachalek Answers 的组合 - 您不能将远程页面添加到主屏幕。 However, you can redirect from the page after it has been added to the home screen.但是,您可以在页面添加到主屏幕后从页面重定向。

All you need to do is use this simple javaScript:您需要做的就是使用这个简单的 javaScript:

if ("standalone" in window.navigator && window.navigator.standalone){ //checks if you're in app mode

  window.location = 'http://www.example.com';  //the URL you want to refer to.

}

Make sure you add this html code to your page:确保将此 html 代码添加到您的页面:

<meta name="apple-mobile-web-app-capable" content="yes">

No, without being jailbroken (and there is nothing that I know of that achieves this) , there is no way to edit the actual URL.不,如果没有越狱(据我所知,没有任何东西可以实现这一点) ,就无法编辑实际的 URL。

Apple restricts this for at least one reason I can think of Security . Apple 至少出于我能想到Security的一个原因限制了这一点。 Editing the URL would allow people to use javascript, which would inevitably lead to malware.编辑 URL 将允许人们使用 javascript,这将不可避免地导致恶意软件。

I got this to work for my purpose of populating the URL of a "share" bookmark on iPhone:为了在 iPhone 上填充“共享”书签的 URL,我得到了这个工作:

history.pushState('','','/');

This worked since I only needed to cut the URL to the home domain.这很有效,因为我只需要将 URL 剪切到主域。 (And I used document.title="newName" to set the screen name of the bookmark icon.) (并且我使用 document.title="newName" 来设置书签图标的屏幕名称。)

This didn't exist back when the question was asked, but the start_url field in the manifest allows this:当问题被问到时,这并不存在,但清单中的 start_url 字段允许这样做:

https://developer.mozilla.org/en-US/docs/Web/Manifest/start_url https://developer.mozilla.org/en-US/docs/Web/Manifest/start_url

Unfortunately this doesn't allow dynamic control, since the manifest will usually be fetched at the start.不幸的是,这不允许动态控制,因为通常会在开始时获取清单。 For example, it doesn't allow having it remember the search the user was on but remove the page number, so the saved search doesn't start three pages down when it's opened later.例如,它不允许它记住用户进行的搜索但删除了页码,因此保存的搜索在稍后打开时不会从三页开始。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM