UIWebView does not respond to calls in "applicationWillResignActivity" - javascript

I am developing a web-app in iOS using PhoneGap. I need to make a javascript function call when the application goes into the background.
I am calling the function from "applicationWillResignActivity" method, but its not executing till the app comes to the foreground next time. Any other non-javascript code is getting executed. This happens only when I press home button. In other cases, like pressing power button when the app is running, and pressing the home button twice, the javascript function gets executed.
Is there some constraint over UIWebView of such kind? Can someone please tell if there is a way to make javascript work when user presses Home button?

It seems there is no way to capture such event. So just to self-answer this question, that there indeed is no delegate method to run JS code when app goes into background.

Related

How can I debug calls to window.open?

I have some React app here that has a malfunction that causes the page to open a new tab with itself. Recursive. and that is rather annoying as the number of tabs runs quickly into an out of memory situation. I want to debug the code to see the stack when the window.open call happens. I do not know where in the application the call happens and so wonder if there is a way to trigger Chrome to jump into script debug mode when something wants to open a window/tab?
So you can use the debugger of chrome, and then add some breakpoint to exactly decide when the code should stop, and then you use the control to jump to the next execution and decide when to go a step back and forth.
it's available for free, all you need to do is to inspect your React app and then visit the Sources tab, there you will see the code in javascript and you can start adding breakpoint and so.
You can also add mouse event listener , like click , dbclick...
You can also trigger and debug how a specific function si running.

Cordova `pause` event on iOS

Is there any Cordova/DOM event that I can hook into on iOS when the user directly closes the app (by double-tapping the Home button and then swiping the app away)? The pause event does successfully fire when the Home button is pressed once and the app is sent to the background, but the double-tap+close seems not to do this -- at least in the emulator.
I am using the pause event to capture and store app state, so closing without saving will leave the user with no previous state to return to, or worse, an old state.
I am aware of the "iOS Quirks" warning in the documentation that says:
In the pause handler, any calls to the Cordova API or to native plugins that go through Objective-C do not work, along with any interactive calls, such as alerts or console.log(). They are only processed when the app resumes, on the next run loop.
...but unless someone corrects me, I don't think this is the issue here
This question has been asked a lot on ionic forum and the consensus at the moment appears to be that this is not possible. I had this same desire for a time keeping app. I ended up deciding to use setInterval at a frequency acceptable (for me 3 seconds was fine). Agree this should be a feature

how to call or mimic a javascript function inside a webview

I am making a hybrid app. I am using xamarin for android, pretty much the same as android. I have already figured out how to hook the phone's back button press. When pressed I want my app code to either mimic or call a javascript function that is part of the webpage that the webview is displaying. It is my web page, so I know the code that the webview is rendering. To be specific, I want to call a jquery slideToggle function on a page element when the phone's back button is pressed. Can that be done, and if so what would be the best approach? I'm hoping that someone here has had to do something just like this in the past. Thanks.
I don't think webview supports JQuery, unless maybe you reference it.
The Xamarin android way of doing it is:
webView.addJavascriptInterface(new JsObject(), "injectedObject");
webView.loadData("", "text/html", null);
webView.loadUrl("javascript:alert(injectedObject.toString())");
Source : Android.Webkit.WebView.AddJavascriptInterface Method
Also make sure you have enabled Javascript,
web_view.Settings.JavaScriptEnabled = true;
You can show or hide the element by finiding it by id (document.FindElementById). Then you can set its display to block or none. Thus you can do it purely in Javascript.
Add on :
There are couple of problems with your method. To name a few -
on pressing of back button user expects a particular behavior and it should not be altered. This would not give a rich user experience.
if you are override ing the back button press then what about the navigation bar menu click.
JavaScript way of doing can cause security issues.

Chrome executing all JS again on browser back button

I am developing a web application. And I wrote some JS script to be executed on document ready. But in chrome when we click on back button and go back to previous page it is executing all the js script again. But when I use same on firefox it do not execute the JS.
I have an accordion on a page and when user open any accordion and go on one of the link under the accordion and after that if again clicks the back button on the accordion page chrome is closing all the accordions as I have written the script to close all these on document ready. But firefox do not close.
Is there any way to fix this with javascript? So that I can put any condition like if(history.forward.length < 1){ do this....}
You can use the pageshow event to guarantee you always detect navigation to a particular page, regardless of whether the user presses the back/forward button or selects a link, and regardless of which browser is being used.
Then you can perform checks regarding the state of UI and perform logic as required (i.e. modify UI, prevent execution of additional JS).
window.addEventListener('pageshow', function(event) {
// check state of UI, etc.
});
The solution that came to my mind is using sessionStorage to know if it is a first time loading or not. Or even better, you can keep state of your accordions in session storage so it always be the way the user want.
In my case, the iframe was a hidden iframe (width and height zero).
This iframe is just an workaround from legacy system, developed 12 years ago. But still using nowadays on current application.
To solve it, i just redirected the page loaded into iframe to the blank page.
Example:
page_loaded_into_iframe.php
<?php
//do the php stuffs
?>
<script>
alert("hello world");
location.href = "about:blank"; // here, where the the magic happens!
</script>
Once pressed the "back button", the browser will reload a blank page.
Be aware that this might be not applicable if your case is not similar to mine.
In the Chrome Extension you can use the function:
chrome.webNavigation.onCommitted.addListener(function callback)
and in the callback function you may take a look to the arguments:
transitionType + transitionQualifiers
to look for:
"forward_back" The user used the Forward or Back button to initiate the navigation.
For deatils see chrome.webNavigation
Of course, this event can be communicated to the content script with the usual message model (refer to: Message Passing

Window 8 JavaScript app oncheckpoint not working

I am porting a html5 game to win8. To save the game state, I call a function save_game (which uses localStorage to store some data) in window.unload, which of course does not work here. So I use WinJS.Application.oncheckpoint instead. Strangely, if I launch a game and press alt-f4, the game state is not saved. Debugging in VS with console.log in the event handler, I found that it seems to be triggered only when I resume the app. Bizarrely, if I put a break point in the code, the event handler will then be correctly executed during suspension.
Anyone has any idea why this happen? Is this a bug in win8?
Thanks in advance.
You are seeing known behavior when debugging your app with VS. When you close an app via user action (Alt-F4 / top swipe), the app is actually held open for a while by the debugger. If you set a breakpoint in oncheckpoint, debug app, press Alt-F4 and then WAIT for ~5 seconds or so, you will hit the breakpoint. The best way to simulate a user "close" event is to use the Suspend and Shutdown option from within VS that will immediately fire oncheckpoint.

Categories