javascript - 我可以检测我的 PWA 是作为应用程序启动还是作为网站访问?
问题描述
如果我有 PWA,我可能想要求我的用户将其添加到他们的启动器中,但我不想问它是否实际上是从启动器启动的。
有没有办法从 javascript 中检测到这一点?
解决方案
对于 android,您应该只在收到beforeinstallprompt
事件后提示用户安装。仅当尚未安装 PWA 时才会触发此事件。
window.addEventListener('beforeinstallprompt', (e) => {
e.preventDefault();
deferredPrompt = e;
// Update UI notify the user they can add to home screen
btnAdd.style.display = 'block';
});
https://developers.google.com/web/fundamentals/app-install-banners/
对于 IOS,您可以检查window.navigator.standalone
,如果该应用程序已安装,则应为 true。
// 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 (isIos() && !isInStandaloneMode()) {
// offer app installation here
}
https://www.netguru.co/codestories/few-tips-that-will-make-your-pwa-on-ios-feel-like-native
推荐阅读
- python - Psycopg2 无法在 Windows 中导入
- json - 我在哪里可以为 Roku 存储一个 Json 来解析?
- terraform - Terraform:多个 ACM 证书
- angularjs - Angular 中的 Firebase 身份验证(护目镜)
- javascript - 根据视图更改菜单
- java - 从光束管道写入 tfrecords?
- r - 如何根据相等的长度拆分R中的字符串列并将它们放在不同的行中
- ruby-on-rails - "\xC3" 到 UTF-8 从 ASCII-8BIT 到 UTF-8 到 ISO-8859-1 的转换
- javascript - Navigator.vibrate() 在笔记本电脑上不起作用
- json - SWIFT Json 检查对象标题是否存在