是否可以使用JavaScript来模拟移动Safari书签菜单中的“添加到主屏幕”选项?
可能类似于IE的window.external.AddFavorite(location.href, document.title);
吗?
是否可以使用JavaScript来模拟移动Safari书签菜单中的“添加到主屏幕”选项?
可能类似于IE的window.external.AddFavorite(location.href, document.title);
吗?
在Safari实现Service Worker并遵循Chrome和Firefox所制定的方向之前,无法通过编程方式将应用程序添加到主屏幕,也无法让浏览器提示用户。
然而,有一个小型库可以提示用户这样做,并且甚至指向正确的位置。非常有效。
在MobileSafari中添加任何书签的唯一方法(包括主屏幕上的书签)是使用内置UI,并且苹果没有提供从页面内部脚本执行此操作的任何方式。 实际上,我非常确定在Safari桌面版中也没有进行此操作的机制。
window.navigator.standalone
,如果在移动Safari中运行,则敦促他们添加它。 - Kerrick有一个开源的JavaScript库提供了相关功能:
mobile-bookmark-bubble移动书签气泡是一个JavaScript库,它向您的移动网络应用程序底部添加一个推广气泡,邀请用户将应用程序添加到其设备的主屏幕。该库使用HTML5本地存储来跟踪是否已经显示了推广信息,以避免不断地打扰用户。
此库的当前实现专门针对Mobile Safari,即iPhone和iPad设备上使用的Web浏览器。
2020年,移动Safari仍然无法实现此功能。
下一个最佳解决方案是显示添加页面到主屏幕的步骤说明。
// Check if user has seen the message already
const hasSeenInstallPopup = localStorage.getItem("hasSeenInstallPopup");
// Detects if device is on iOS
const isIos = () => {
const userAgent = window.navigator.userAgent.toLowerCase();
return /iphone|ipad|ipod/.test( userAgent );
}
// Detects if device is in standalone mode
const isInStandaloneMode = () => ('standalone' in window.navigator) && (window.navigator.standalone);
// Checks if should display install popup notification:
if (!hasSeenInstallPopup && isIos() && !isInStandaloneMode()) {
showInstallMessage();
localStorage.setItem("hasSeenInstallPopup", true);
}
window.navigator.standalone
正常工作,所以我使用return (window.matchMedia('(display-mode: standalone)').matches);
来检查应用程序是否运行在独立模式下。 - Tintin81https://github.com/h5bp/mobile-boilerplate/wiki/Mobile-Bookmark-Bubble
简而言之:@Craig在上面给出了更好的答案。
以下是我的原始回答,但我不认为它能充分回答问题。今天,您需要尝试使用库来模拟此效果,因为A2HS不支持WebViews(PWAs)。
@Kerrick我想删除我的答案,但无法删除,因为它已被接受。
旧答案:
是的。大多数现代浏览器都支持渐进式Web应用程序的“添加到主屏幕”(或A2HS)功能。引用Mozilla Web Docs文章的话:
添加到主屏幕是现代浏览器中可用的一项功能,允许用户“安装”Web应用程序,即将其快捷方式添加到主屏幕。
在移动/桌面Safari上似乎仍然不可能...
可以使用beforeinstallprompt事件来触发安装提示。 此功能在iOS上不受支持。首先,使PWA可安装。
// Initialize deferredPrompt for use later to show browser install prompt.
let deferredPrompt;
window.addEventListener('beforeinstallprompt', (e) => {
// Prevent the mini-infobar from appearing on mobile
e.preventDefault();
// Stash the event so it can be triggered later.
deferredPrompt = e;
});
buttonInstall.addEventListener('click', async () => {
if (!deferredPrompt) {
return;
}
// Show the install prompt
deferredPrompt.prompt();
// Wait for the user to respond to the prompt
const { outcome } = await deferredPrompt.userChoice;
// We've used the prompt, and can't use it again, throw it away
deferredPrompt = null;
});
React应用程序上下文的示例:
const AddToHomeScreen = () => {
const [deferredPrompt, setDeferredPrompt] = useState(null)
const handleBeforeInstallPrompt = useCallback((e) => {
// Prevent the mini-infobar from appearing on mobile
e.preventDefault()
// Store the event for later use
setDeferredPrompt(e)
}, [])
const handleAddToHomeScreenClick = async () => {
if (deferredPrompt) {
// Show the install prompt
deferredPrompt.prompt()
const choiceResult = await deferredPrompt.userChoice
if (choiceResult.outcome === "accepted") {
console.log("User accepted the install prompt")
} else {
console.log("User dismissed the install prompt")
}
setDeferredPrompt(null)
} else {
console.log("Install prompt is not available")
}
}
useEffect(() => {
window.addEventListener("beforeinstallprompt", handleBeforeInstallPrompt)
return () => {
window.removeEventListener("beforeinstallprompt", handleBeforeInstallPrompt)
}
}, [handleBeforeInstallPrompt])
return (
<button onClick={handleAddToHomeScreenClick}>
Add to Home Screen
</button>
)
}
export default AddToHomeScreen
对于iOS,有一种替代方法。您可以引导用户通过将应用程序添加到其主屏幕,类似于在add-to-homescreen repo中所做的方式:
我只是添加了一些代码:
// Detects if device is on iOS
const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent)
else if (isIOS) {
// Here we could notify the user to guide them in order to add the app to Home Screen.
// You can update your UI to render a notification, popin, alert...
console.log(
"To add this web app to your Home Screen, open this app in Safari/Chrome and tap the Share button, then select 'Add to Home Screen'."
)
}
import { useEffect, useState, useCallback } from "react"
const AddToHomeScreen = () => {
const [deferredPrompt, setDeferredPrompt] = useState(null)
const handleBeforeInstallPrompt = useCallback((e) => {
// Prevent the mini-infobar from appearing on mobile
e.preventDefault()
// Store the event for later use
setDeferredPrompt(e)
}, [])
const handleAddToHomeScreenClick = async () => {
const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent)
if (deferredPrompt) {
// Show the install prompt
deferredPrompt.prompt()
// Wait for the user to respond to the prompt
const choiceResult = await deferredPrompt.userChoice
if (choiceResult.outcome === "accepted") {
console.log("User accepted the install prompt")
} else {
console.log("User dismissed the install prompt")
}
setDeferredPrompt(null)
} else if (isIOS) {
// Here we could notify the user to guide them in order to add the app to Home Screen.
// You can update your UI to render a notification, popin, alert...
console.log(
"To add this web app to your Home Screen, open this app in Safari and tap the Share button, then select 'Add to Home Screen'."
)
} else {
console.log("Install prompt is not available")
}
}
useEffect(() => {
window.addEventListener("beforeinstallprompt", handleBeforeInstallPrompt)
return () => {
window.removeEventListener("beforeinstallprompt", handleBeforeInstallPrompt)
}
}, [handleBeforeInstallPrompt])
return (
<button onClick={handleAddToHomeScreenClick}>
Add to Home Screen
</button>
)
}
export default AddToHomeScreen
我希望它能帮助到某些人。祝你有美好的一天!