Ionic/Cordova iOS - invoke function on app open - ios

Is it possible to invoke a function after applications is re-opened (closed with home button, not closed as a process)?

Yes, you can do this using by injecting the $ionicPlatform service into either a run block (for the entire app) or into a single controller.
Here is an example of it in a run block:
.run(function($ionicPlatform) {
$ionicPlatform.on('resume', function(){
// Do sweet stuff!
});
}
And here it is in a controller:
.controller(function($ionicPlatform) {
$ionicPlatform.on('resume', function(){
// Do sweet stuff!
});
}
Note that this is really just a wrapper for Cordova's resume lifecycle event. You can see more information about the $ionicPlatform service and the 'on' method in the Ionic framework docs here.

Related

Sending commands to the web app in electron renderer process since contextIsolation changes

I understand that contextIsolation changes are introduced for security purposes, and i read about contextBridge which exposes custom API to the webapp running in the renderer process so web app can control electron app in which is runing.
electron preload script
const contextBridge = require("electron").contextBridge;
contextBridge.exposeInMainWorld("electronApi", {
'doSomething' : function () {
// some code to execute
}
});
web app
window.electronApi.doSomething();
This is perfectly clear and i understand why is this done this way.
However, i do not understand how can communication work the other way, so how can electron execute web app commands? Let's take following example, web app has window.someWebAppMethod defined and electron should execute it
web app
window.someWebAppMethod = function () {
// do somehing web app related
}
electron preload script
window.someWebAppMethod()
^^ this does not work because of contextIsolation which was whole point of contextIsolation, but i still need to have a certain way of triggering web app commands from electron. Most obvious reason is let's say i have electron main menu with command labeled "Open Quick Jump" which should tell the web app loaded in rendered process to execute method which will show the "Quick Jump" function of the web app.
Maybe i'm missing something painfully obvious, but i'd still appreciate any help i can get.
Thanks
I found a way of doing this. Not sure it's obvious or if it is secure enough, but here it is:
electron preload script
const contextBridge = require("electron").contextBridge;
let doSomethingInWebApp = null;
contextBridge.exposeInMainWorld("electronApi", {
'exposeDoSomethingInWebApp' : function (callback) {
doSomethingInWebApp = callback;
}
});
web app
if (window.electronApi && window.electronApi.exposeDoSomethingInWebApp) {
window.electronApi.exposeDoSomethingInWebApp(function () {
// execute whatever you need to execute in webapp
});
}
electron preload script
if (doSomethingInWebApp) {
// execute previously defined custom behavior in web app
doSomethingInWebApp();
}
So it's quite simple and it works.

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

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;
});
}

KendoUI mobile app, view event afterShow has no access to cordova plugin. Outside event is fine

I have a kendoui applbuilder mobile app. I have installed a custom camerapreview plugin and it works fine. I tried adding an event handler to my view (afterShow) to set something in the camera plugin module:
cordova.plugins.camerapreview.startCamera(
which initializes the camera preview.
the problem seems to be that in this handler cordova.plugins.camerapreview is undefined? Access to this same method in a button handler on the view works fine. I'm assuming this has something to do with dependency? How can i ensure this is loaded? Doesn't make sense to me that it wouldn't be available after the view has loaded and bound the model.
my code looks like:
// Handle "deviceready" event
document.addEventListener('deviceready', onDeviceReady, false);
var mobileApp = new kendo.mobile.Application(document.body, {
skin: 'flat',
initial: 'views/home.html'
});
When using Kendo UI Mobile app with Cordova, make sure to initialize the app in the deviceready event. This will ensure that the Cordova APIs will be available throughout the whole app lifecycle.
// this function is called by Cordova when the application is loaded by the device
document.addEventListener('deviceready', function () {
// hide the splash screen as soon as the app is ready. otherwise
// Cordova will wait 5 very long seconds to do it for you.
navigator.splashscreen.hide();
app = new kendo.mobile.Application(document.body, {
// you can change the default transition (slide, zoom or fade)
transition: 'slide',
// comment out the following line to get a UI which matches the look
// and feel of the operating system
// skin: 'flat',
// the application needs to know which view to load first
initial: 'views/home.html'
});
}, false);

PhoneGap navigator.compass.getCurrentHeading called multiple times on iPhone

I would appreciate any help in solving this - or at least where to look to solve it.
What I have is calling on iPhone navigator.compass.getCurrentHeading(succ, fail), the success function is called every time the device is moved even slighly. In the XCode debug log I see lots of entries of navigator.compass.setHeading calls being generated for every movement. If I try to poll for heading data again - the request just hangs. Here's the code:
function onBodyLoad() {
if (typeof navigator.device == "undefined") {
document.addEventListener("deviceready", onDeviceReady, false);
} else {
onDeviceReady();
}
}
function succ(heading) {
alert("compass " + heading);
}
function fail() {
alert('fail');
}
function onDeviceReady() {
navigator.compass.getCurrentHeading(succ, fail);
}
This is really strange behaviour, as I expect getCurrentHeading to be called just once and return a single result, instead of the unstoppable flurry of events.
I use PhoneGap 1.0.0. The same code on Android works perfectly. I've removed all custom JS code to prevent possibility of conflicts.
It is odd that noone else seems to encounter this. In any case, this (hacky) solution may help anyone who comes looking for an answer.
We had to stop using getCurrentHeading because of this issue, and replaced it with navigator.compass.watchHeading instead. On clearing the watch we also call navigator.compass.stop() function to prevent from further compass spamming (for iPhone platform only - Android is fine), and before calling watchHeading again we call navigator.compass.stop() and navigator.compass.start(), to reinitialize the compass "just in case" (again, on iPhone only).
After taking these measures the page that user compass no longer hangs on second entry, and there is no heading spamming outside of this page.

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