callback function attached to window event not executed after opening the iOS Control Center - ios

In my ionic application for IOS I am listening to a window event generated by a cordova plugin.
Here is the code that I use for listen to the event and perform an action.
window.addEventListener('event', (event) => {
...
console.log("event received");
doSomething();
});
doSomething(){console.log("perform an action");}
Everything work and I am able to receive the event until I open the IOS Control Center (swipe up from the bottom). After I close the Control Center again I can see that the event is logged ("event received") but the function doSomething() is never called.
Someone encountered a similar situation?

Since the event is generated outside angular, I needed to call ngZone.run in order to let angular know that something happend and so trigger the change. I thid it this way
constructor(private zone: NgZone) {}
ngOnInit(){
window.addEventListener('event', (event) => {
this.ngZone.run(() => {
do stuff;
});
}

Related

Closing application and notifying renderer process

I have an Electron application that needs to save some data when it's closed by the user (e.g. just after the user clicked on the "Close" button).
The data is available at the renderer process, so it should be notified before the application dies.
The Electron API for Browser Window mentions a close method, but it seems this is done by the main process, not the renderer one (if I'm not mistaken).
I tried using WebContents.send from the main process to notify the renderer process, but it seems that, because the message is asynchronous, the application is closed before the renderer process has the time to actually perform the operations.
You can just use the normal unload or beforeunload events in the renderer process:
window.addEventListener('unload', function(event) {
// store data etc.
})
So far, the simplest solution that worked for me consists in doing the following:
On the main process, the BrowserWindow listens on the close event, and when it happens, it sends a message via webContents to the renderer process. It also prevents the application from being immediately closed by calling event.preventDefault();
The renderer process is always listening on IPC messages from the main process, then when it receives the close event notification, it saves its data, then sends the main process an IPC message (e.g. closed);
The main process has previously set a hook to listen to the renderer IPC messages (ipcMain.on), so when the closed message arrives, it finally closes the program (e.g. via app.quit()).
Note that, if I understood it correctly, calling app.quit() sends another close event to the BrowserWindow, so it will loop unless you prevent it somehow. I used a dirty hack (quit the second time the close event is called, without calling event.preventDefault()), but a better solution must exist.
On the Main process:
const ipc = require('electron').ipcMain;
let status = 0;
mainWindow.on('close', function (e) {
if (status == 0) {
if (mainWindow) {
e.preventDefault();
mainWindow.webContents.send('app-close');
}
}
})
ipc.on('closed', _ => {
status = 1;
mainWindow = null;
if (process.platform !== 'darwin') {
app.quit();
}
})
On the renderer process:
const electron = require('electron');
const ipc = electron.ipcRenderer;
ipc.on('app-close', _ => {
//do something here...
ipc.send('closed');
});

Which events to trap on taphold

I listen for the taphold event on an element and then open a popup with choices of actions. The problem is, after the popup is opened, new mouse/finger events are triggered. So my solution is to trap all the subsequent mouse/finger events until the touchend event:
function tapholdTriggered() {
$.mcm.mobile.$d.on('vclick.taphold vmousedown.taphold click.taphold mousedown.taphold tap.taphold taphold.taphold touchstart.taphold touchmove.taphold', function (event) {
event.preventDefault();
event.stopImmediatePropagation();
})
.on('touchend.taphold', function (event) {
event.preventDefault();
event.stopImmediatePropagation();
$.mcm.mobile.$d.off('.taphold');
});
}
So basically I would listen for the taphold event, call tapholdTriggered(), then open the popup.
My question/issue is that I think I am excessively trapping events. I don't know what order the various mouse/finger events are fired in. So if someone could help me optimize the trapped events, I'd appreciate it.
Thanks.
I accomplished this a little differently using a screen that covers the body and then gets removed on touchend, mouseup, and vmouseup.

Appcelerator: Event when reopening App

I want my app to reload data when it will be reopened (from iOS "Multitasking").
I've tested:
Ti.UI.addEventListener('reload', function() {
alert('reloaded app');
});
but this event just gets fired when the App will be opened the first time.
The app entering the foreground is an app level event. So you need to register on Ti.App, not Ti.UI. In fact I can't find any reference to the event you are using.
Ti.App.addEventListener('resume', function() {
alert('reloaded app');
});
Or you can use "resumed" for after it has completely returned.
See this page

iPad HTML audio detect `load`

I want to detect when audio has loaded on iPad. My code loads, and plays the mp3, but the event listener never fires.
$mp3.load()
$mp3.addEventListener("load", function() {
alert('Happy days') // <~~ this never fires
}, true)
$mp3.play()
I am using iOS 4.2. I am aware that all of this might not work on the latest iOS, and I don't mind that.
You need to add an event listener for canplaythrough, e.g.
addEventListener("canplaythrough", this.onLoad.bind(this), false);
Then once it triggers, remove it so you wont get it again:
onLoad:function ()
{
arguments.callee.removeEventListener("canplaythrough", this.onLoad.bind(this), false);
// do something
}

PhoneGap iOS application 'deviceready' event - initial entry vs resume

I am using PhoneGap to create a native iOS app. The app implements an iOS scheme so that it can be invoked from mobile Safari like myapp://?parameters.
The app activities depend on the input parameters, i read those by handling the 'deviceready' event.
The problem is that after initial execution the app remains in the background, and any subsequent calls (from the browser) do not fire another 'deviceready', and as a result i cannot get the new parameters.
Any ideas? Thanks!
Did you manage to get the resume event to fire in the end?
I'm having trouble with this as well - I have the following code:
window.addEventListener('load', function () {
document.addEventListener('deviceready', onDeviceReady, false);
}, false);
function onDeviceReady() {
document.addEventListener('resume', onResume, false);
document.addEventListener('pause', onPause, false);
document.addEventListener('online', onOnline, false);
document.addEventListener('offline', onOffline, false);
}
function onResume() {
alert('resume');
}
function onPause() {
alert('pause');
}
function onOnline() {
alert('online');
}
function onOffline() {
alert('offline');
}
And although the deviceready and online events appear to be firing, I can't seem to get the resume event to fire. Any light anyone could shed on this would be much appreciated!
There is a 'resume' event. http://docs.phonegap.com/phonegap_events_events.md.html
It has been more than a year since the question was asked, but I want to reply for future readers anyway.
If you want your app to respond to url schemes after the initial load( on resume ), you need to define a function called handleOpenURL() in global context. Then this function will automatically be fired when your app resumes.
function handleOpenURL(invokeString) {
// do something with the invoke string
}

Resources