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

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
}

Related

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

Ionic/Cordova iOS - invoke function on app open

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.

Hide unwanted alerts

i am working on cordova app for iOS in which i am getting user current location so that ask me about some message like that would you allow current position with options allow or don't allow. its fine but after that it again ask me like that show in image.
How do i stop that message? or maybe change its text?
also it second alert in first aper it just show me app name with confirmation about getting current location but after that this one appears again.
Any help?
To remove the alert, you have return the device's current position to the geolocationSuccess callback with a Position object as the parameter after the device is ready.
Use the below method. It will returns the device's current position as a Position object.
navigator.geolocation.getCurrentPosition(geolocationSuccess,
[geolocationError],
[geolocationOptions]);
Example:
$(function(){
document.addEventListener("deviceready", onDeviceReady, false);
})
function onDeviceReady() {
navigator.geolocation.getCurrentPosition(onSuccess, onError);
}
function onSuccess(position) {
// callback here
}
function onError(error) {
// callback here
}
Please look at the phonegap documentation.

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 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.

Resources