BACKGROUND:
I am running a phonegap app that I am building using phonegap build (1.6.1).
android build seems to work just fine,
however on iOS (Iphone 3GS 16M iOS: 5.0.1 (9A405) ) I never get the deviceready event.
I am trying to figure out what am I missing here.
QUESTIONS:
1. Does it matter if the binding of the deviceready event comes before or after including phonegap.js ?
2. Could it be that the event is happening “too early” and my handler is not yet bound to it ?
3. Is there a polling based way to figure out that the device is ready?
4. Where should handling of deviceready be done relating to JQuery’s $(document').ready ?
MY CODE:
my current code tries to synchronize 3 things:
1. JQuery’s .ready event
2. PhoneGap’s deviceready event
3. (careful) polling results on window.navigator.device.platform
in the following way:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="./scripts/jquery-1.7.2.js"></script>
<script type="text/javascript" src="./scripts/jquery.myapp.js"></script>
<script type="text/javascript">
window.myapp={
deviceReady:false,
JQueryReady:false,
started:false,
logbuf:[]
}
function log(m){
window.myapp.logbuf.push(m);
console.debug(window.myapp.logbuf.join('||'));
var el=document.getElementById('container');
if (el!=null) {
el.innerHTML+=', ' + window.myapp.logbuf.join('||') ;
window.myapp.logbuf=[];
}
}
//Wait for device
function onDeviceReady() {
window.myapp.deviceReady=true;
log('phonegap deviceready event fired');
startIfAllReady();
}
document.addEventListener("deviceready", onDeviceReady, false);
function pollDevice(){
if (window && window.navigator && window.navigator.device && window.navigator.device.platform ) {
log('polling found device');
window.myapp.deviceReady=true;
startIfAllReady();
}
else {
log('polling');
window.setTimeout(arguments.callee,500);
}
}
pollDevice();
//Wait for jquery
$(document).ready(
function(){
window.myapp.JQueryReady=true;
log('jquery document.ready event fired');
startIfAllReady();
}
);
function startIfAllReady(){
log('startIfAllReady window.myapp.JQueryReady=' + window.myapp.JQueryReady + ' window.myapp.deviceReady=' + window.myapp.deviceReady + ' window.myapp.started=' + window.myapp.started );
if (window.myapp.deviceReady && window.myapp.JQueryReady && window.myapp.started==false) {
log('starting');
started=true;
renderClient();
}
else {
log('cant start');
}
}
function renderClient(){
log('renderClient called - RETURNING !');
return;
window.setTimeout(function(){window.scrollTo(0, 1)},100);
log('create myappClient');
$('#myappClientContainer').empty().myappClient(
{width:$(window).width(),
height:$(window).height()
}
);
}
</script>
<script type="text/javascript" src="phonegap.js"></script>
<link rel="stylesheet" type="text/css" href="./styles/client.css" />
<style>
html , body { margin:0px; background-color:gray;}
</style>
<title>myapp</title>
</script>
</head>
<body style="width:100%">
<script>
log('body script executing');
</script>
<div style="width:100%" id="myappClientContainer">DEFAULT HTML</div>
</body>
</html>
CODE RESULTS:
in a regulat browser (chrome) I get:
DEFAULT HTML polling||body script executing||jquery document.ready event fired startIfAllReady window.myapp.JQueryReady=true window.myapp.deviceReady=false window.myapp.started=false cant start polling polling polling ... (and polling....)
on Iphone:
DEFAULT HTML polling||body script executing||jquery document.ready event fired startIfAllReady window.myapp.JQueryReady=true window.myapp.deviceReady=false window.myapp.started=false cant start
As you can see – no “phonegap deviceready event fired” anywhere, nor does the polling seem to work 🙁
notice that on the iphone, only single “polling” log entry at the start…
can’t iPhone handle onTimeout ?
why dont I get the deviceready event ?!
Another thing I noticed:
When (phonegap)building with debug set,
I can see the device/app in the PhoneGapBuild weinre debug window for some short time and then it just disappears.
Thanks
UPDATE:
my polling loop above is wrong,
It should look for window.navigator.platform
anyway I dropped it all together.
something Is just not working.
seems like i neglected to update this Q.
anyway the probelm was that ‘console.debug’ does not exist within winere and so my code was failing.
Eyal