Reputation: 71
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...
Upvotes: 7
Views: 2034
Reputation: 225
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
Upvotes: 1
Reputation: 6842
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
Upvotes: 2