用于“添加到主屏幕”的 Javascript 在 iPhone 上?
是否可以使用 Javascript 模拟 Mobile Safari 书签菜单中的“添加到主屏幕”选项?
类似于 IE 的 window.external.AddFavorite(location.href, document.title);
可能吗?
Is it possible to use Javascript to emulate the Add to Home Screen option in Mobile Safari's bookmark menu?
Something similar to IE's window.external.AddFavorite(location.href, document.title);
possibly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
在 Safari 实现 Service Worker 并遵循 Chrome 和 Firefox 设置的方向之前,无法以编程方式将应用程序添加到主屏幕,或者让浏览器提示用户。
但是,有一个小型库可以提示用户执行以下操作:它甚至指向正确的位置。 工作是一种享受。
https://github.com/cubiq/add-to-homescreen
Until Safari implements Service Worker and follows the direction set by Chrome and Firefox, there is no way to add your app programatically to the home screen, or to have the browser prompt the user
However, there is a small library that prompts the user to do it and even points to the right spot. Works a treat.
https://github.com/cubiq/add-to-homescreen
在 MobileSafari 中添加任何书签(包括主屏幕上的书签)的唯一方法是使用内置 UI,而 Apple 无论如何也不提供通过页面内的脚本执行此操作的方法。 事实上,我很确定在桌面版 Safari 上也没有执行此操作的机制。
The only way to add any book marks in MobileSafari (including ones on the home screen) is with the builtin UI, and that Apples does not provide anyway to do this from scripts within a page. In fact, I am pretty sure there is no mechanism for doing this on the desktop version of Safari either.
到 2020 年,这在 Mobile Safari 上仍然无法实现。
下一个最佳解决方案是显示有关将网站添加为用户主屏幕快捷方式的说明。
In 2020, this is still not possible on Mobile Safari.
The next best solution is to show instructions for adding the website as a shortcut to the user's home screen.
有一个开源 Javascript 库提供了相关的东西:
mobile-bookmark-bubble
There is an open source Javascript library that offers something related :
mobile-bookmark-bubble
在 javascript 中,这是不可能的,但在“Web Clips”的帮助下,我们可以在 iPhone 中创建“添加到主屏幕”图标或快捷方式(通过 .mobileconfig 的代码文件)
https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual /iPhoneOTAConfiguration/ConfigurationProfileExamples/ConfigurationProfileExamples.html
http://appdistro.cttapp.com/webclip/< /a>
创建 mobileconfig 文件后,我们可以在 iphone safari 浏览器安装证书中传递此 url,完成后检查你的 iphone 主屏幕,有一个网页或 web 应用程序的快捷方式图标。
In javascript, it is not possible but yes with the help of “Web Clips” we can create a "add to home screen" icon or shortcut in iPhone( by the code file of .mobileconfig)
https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/iPhoneOTAConfiguration/ConfigurationProfileExamples/ConfigurationProfileExamples.html
http://appdistro.cttapp.com/webclip/
after create a mobileconfig file we can pass this url in iphone safari browser install certificate and after done it check your iphone home screen there is a shortcut icon of your Web page or webapp..
这也是另一个很好的主屏幕脚本,支持 iphone/ipad、Mobile Safari、Android、Blackberry 触摸智能手机和 Playbook。
https://github.com/h5bp/mobile-boilerplate/wiki/Mobile -书签-气泡
This is also another good Home Screen script that support iphone/ipad, Mobile Safari, Android, Blackberry touch smartphones and Playbook .
https://github.com/h5bp/mobile-boilerplate/wiki/Mobile-Bookmark-Bubble
TLDR:@Craig 在上面有一个更好的答案。
以下是我的原始答案,但我认为它不能充分回答问题。 今天我想说你必须尝试一个库来模拟这种效果,因为 WebViews (PWA) 不支持 A2HS。
@Kerrick我想删除我的答案,但不能,因为它已被接受。
老答案:
是的。 大多数现代浏览器都支持渐进式 Web 应用程序的“添加到主屏幕”(或 A2HS)功能。 引用 Mozilla Web Docs 文章:
另请参阅:caniuse.com 上的 A2HS 浏览器支持
TLDR: @Craig has a better answer above.
Below is my original answer, but I do not believe it answers the question adequately. Today I would say you'd have to try a library to emulate this effect, as A2HS is not supported in WebViews (PWAs).
@Kerrick I would like to delete my answer, but cannot as it has been accepted.
Old answer:
Yes. The majority of modern browsers support the Add to Home screen (or A2HS) feature for Progressive Web Apps. To quote the Mozilla Web Docs article:
See also: A2HS browser support at caniuse.com
似乎在移动/桌面 Safari 上仍然不可能...
可以使用 beforeinstallprompt 事件。 iOS 不支持此功能。 首先,使 PWA 可安装。
React 应用程序上下文示例:
对于 iOS,有一种替代方法。 您可以引导用户完成将应用程序添加到主屏幕的过程,类似于 add-to-homescreen repo:
我只是添加了一些代码:
我希望它可以帮助别人。 祝你有美好的一天!
It seems that it is still not possible on Mobile/Desktop Safari...
It's possible to trigger the install prompt using the beforeinstallprompt event. This feature is not supported on iOS. First, Making PWAs installable.
Example of a React app context:
For iOS, there's an alternative approach. You can guide the user through the process of adding the app to their Home Screen, similar to what is done in the add-to-homescreen repo:
I just add some codes:
I hope it could help someone. Have a good day!