iOS UiWebView / Page Visibility Api: “pageshow” event not firing

泪湿孤枕 提交于 2019-12-08 19:21:16

问题


I am trying to use the Page Visibility Api to detect when web content is being shown in iOS. I have simple JS:

    window.addEventListener("pageshow", function(){
        alert("page shown");
    }, false);

The alert fires as expected in mobile safari when the tab is made active, or when safari is minimized and then re-shown.

However if I host the web content in a UiWebView, the event is not firing. Has anyone else encountered this issue? If so any work around for this?

Thanks...


回答1:


The UIWebView dose not support that event however you could look into triggering the event your self from the app natively

This how you call a JavaScript function from Objective-C Objective C send data to phonegap and call javascript function

Then you just use the code above to notify the webview when your handling the application being started or restarted from suspension

Objective C: How to check if application is currently active (i.e. user is using it)? should help with that




回答2:


I totally agree the answer above. Even though pageVisibilityChange event cannot been triggered on UIWebview. you can still detect it by native code, then notify the JS by JS-bridge or JS-binding technology.

There is other method to achieve this function by setInterval. You can initialise a variable of present timestamp at the beginning of the app lunching, and then increase this variable by one second in each interval callback func. you need to compare the present timestamp to this variable, if they have difference over one second, it means that the setInterval callback has been blocked! So you can indicate that page has leaved for least serval seconds



来源:https://stackoverflow.com/questions/23232927/ios-uiwebview-page-visibility-api-pageshow-event-not-firing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!