简体   繁体   English

如何使用HTML和JavaScript检测iOS应用何时进入后台

[英]How to detect when iOS app goes the background using HTML and JavaScript

I need to know when my GWT web application goes to the background on an iPad. 我需要知道GWT Web应用程序何时在iPad上进入后台。 The application is not a native iOS app, but rather has a "wrapper" around it allowing it to run as an app. 该应用程序不是本机iOS应用程序,而是周围有一个“包装器”,允许它作为应用程序运行。 The wrapper is written in Swift and uses the UIWebView. 包装器是用Swift编写的,并使用UIWebView。

The application needs to know when the home button is pressed in order to trigger some logic that if a user goes outside of the app then the app should display a warning screen upon returning to the app (ie, receives focus). 应用程序需要知道何时按下主页按钮才能触发一些逻辑,即如果用户离开应用程序外,则应用程序应在返回到应用程序时显示警告屏幕(即,获得焦点)。 This logic should be true no matter how the app loses focus, unless they explicitly exit the app. 无论应用程序如何失去焦点,这种逻辑都应该是正确的,除非它们明确退出应用程序。 I cannot explain why that functionality is necessary due to security purposes, but suffice to say this isn't a general audience type of application; 由于安全性原因,我无法解释为什么需要该功能,但是可以说这不是一般的受众类型的应用程序; there are specific scenarios under which this app is used. 在使用此应用程序的特定情况下。

I've tried using document.hasFocus(), but it always returns true, even though this same logic works on chromebooks, Windows machines, and macbooks. 我尝试使用document.hasFocus(),但即使在chromebook,Windows计算机和macbook上也能使用相同的逻辑,它始终会返回true。

I added visibilityChange listener, but the visibilityState is always visible. 我添加了visibleChanged侦听器,但是visibleState始终可见。 I also added event listeners for pageshow and pagehide. 我还为pageshow和pagehide添加了事件侦听器。 A debug console statement prints out for the pageshow eventlistener, but never for the pagehide eventlistener. 调试控制台语句为pageshow事件侦听器打印,但从不为pagehide事件侦听器打印。 Lastly, I added an event listener for the unload event, but I never see my console statement print out for it. 最后,我为unload事件添加了一个事件侦听器,但是我从来没有看到为它打印出控制台语句。

Based on various searches and articles I've read, it seems mobile Safari ignores most, if not all of these listeners, and the only option I haven't seem to have tried is using timers which I'd prefer to avoid. 根据各种搜索和我读过的文章,似乎移动Safari会忽略大多数(如果不是全部)这些侦听器,而我似乎没有尝试过的唯一选择是使用定时器,而我希望避免使用它。 Also, I need an HTML/Javascript solution as I can only modify my application and cannnot modify the "wrapper" app. 另外,我需要一个HTML / Javascript解决方案,因为我只能修改我的应用程序,而不能修改“包装”应用程序。 I'm using an iPad Pro with iOS 11.4. 我正在将iPad Pro与iOS 11.4配合使用。

Here's just one of the many articles that I referenced: How to detect in iOS webapp when switching back to Safari from background? 这只是我引用的众多文章之一: 从后台切换回Safari时如何在iOS webapp中进行检测?

The relevant code from the application is below. 该应用程序的相关代码如下。 Any thoughts or suggestions are greatly appreciated! 任何想法或建议,不胜感激!

/*************************** page visibility ***************************/
var hidden, visibilityChange;
    if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support
    hidden = "hidden";
    visibilityChange = "visibilitychange";
} else if (typeof document.msHidden !== "undefined") {
    hidden = "msHidden";
    visibilityChange = "msvisibilitychange";
} else if (typeof document.webkitHidden !== "undefined") {
    hidden = "webkitHidden";
    visibilityChange = "webkitvisibilitychange";
}

console.log("debug - common.js - customBlur - visibilityChange : " + visibilityChange + " | hidden: " + hidden);


if (typeof document.addEventListener === "undefined" || hidden === undefined) {
    console.log("This demo requires a browser, such as Google Chrome or Firefox, that supports the Page Visibility API.");
} else {
// Handle page visibility change
    document.addEventListener(visibilityChange, handleVisibilityChange, false);
}

var forceBlur = false;
function handleVisibilityChange() {
if (document.visibilityState == 'hidden') {
    console.log("debug - common.js - handleVisibilityChange - calling delayBlur");
    forceBlur = true;
} else console.log("debug - common.js - handleVisibilityChange - document not hidden");
}

/*************************** pageshow and pagehide ***************************/
var forcePhBlur = false;
window.addEventListener("pageshow", function(evt){
    console.log("debug - common.js - pageshow - showing the page");
}, false);
window.addEventListener("pagehide", function(evt){
    console.log("debug - common.js - pagehide - hiding the page");
    forcePhBlur = true;
}, false);

/*************************** unload ***************************/
var forceUlBlur = false;
window.addEventListener("unload", function(evt){
    console.log("debug - common.js - unload - unloading the page");
    window.onblur = true;
    forceUlBlur = true;
}, false);

/*************************** custom blur logic ***************************/
function delayCheck() {
    console.log("debug - common.js - delayCheck - document.hasFocus(): " +             
    document.hasFocus());
    console.log("debug - common.js - delayCheck - document.hidden: " +        document.hidden);
    if (!document.hasFocus() || document.hidden || forceBlur) {
        delayBlur();  //implemented in the java code
    }
}

var delayVar;

function customBlur() {
console.log("debug - common.js - customBlur - document.hasFocus(): " + document.hasFocus());
console.log("debug - common.js - customBlur - document.hidden: " + document.hidden);
console.log("debug - common.js - customBlur - Document.visibilityState : " + document.visibilityState);
console.log("debug - common.js - customBlur - forceBlur : " + forceBlur);
console.log("debug - common.js - customBlur - forcePhBlur : " + forcePhBlur);
console.log("debug - common.js - customBlur - forceUlBlur : " + forceUlBlur);

if (!document.hasFocus() || document.hidden || forceBlur) {
    console.log("debug - common.js - customBlur - calling delayCheck to blur");
    delayVar = window.setTimeout(delayCheck, 200);
}

Some people went in your same jouney and it could be interesting to dissect their code . 有些人陷入同样的​​困境,剖析他们的代码可能很有趣。

Or you can use Cordova to package your web application into a native app, this will give you ways to access native info through javascript calls. 或者,您可以使用Cordova将您的Web应用程序打包到本机应用程序中,这将为您提供通过javascript调用访问本机信息的方法。

The short of it is I don't think it's possible to tell using javascript when the home button is pushed when using a hybrid app. 简短的是,我认为使用混合应用程序时按下主屏幕按钮时无法使用javascript进行判断。 UI events such as page visibility, unloading, etc are not triggered. 不会触发页面可见性,卸载等UI事件。 For reasons I cannot specify, I cannot use timers. 由于无法指定的原因,我无法使用计时器。 Looks like the only option is to edit the swift code for the iPad wrapper to signal when the application enters the background. 看起来唯一的选择是编辑iPad包装程序的快速代码,以在应用程序进入后台时发出信号。

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

相关问题 当应用程序进入后台时触发JavaScript函数 - Fire javascript function when app goes into background Javascript检测页面何时进入 - Javascript detect when page goes 使用Javascript或HTML检测iOS设备的型号? - Detect model of iOS device using Javascript or HTML? 如何使用 JavaScript 在 HTML5 canvas 上添加圆周运动元素,提供 ZC1C425268E68385D1AB5074F 什么时候接近它? - How to add element with circular motion on HTML5 canvas using JavaScript that provides function when something goes near it? 当应用程序进入后台时,删除DOM元素 - Remove a DOM element when the app goes to the background 从后台切换回Safari时如何在iOS webapp中检测? - How to detect in iOS webapp when switching back to Safari from background? 如何知道反应本机应用程序是否进入后台? - How to know if a react native app goes to background? 如何通过JavaScript检测iPhone何时进入横向模式?有这样的事件吗? - How do I detect when the iPhone goes into landscape mode via JavaScript? Is there an event for this? 使用JavaScript命中时,IOS应用程序未重定向 - IOS App not redirecting when hit using JavaScript 如何使用 Javascript 更改 Html div 背景 - How to change Html div background using Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM