I have the following code:
function onLoad()
{
document.addEventListener("deviceready", onDeviceReady, true);
}
function onDeviceReady()
{
var uuid = device.uuid;
alert(uuid);
}
what am I doing wrong? Android operating normally.
From what I observed ios , is skipping the event deviceready
**I'm using PGB, and my config.xml it's ok*
Your deviceready code looks good to me, but I would suggest you drop the onLoad() function and just call that directly when the script loads.
For example, I created this using the Cordova demo app and it's working well on iOS simulator:
var app = {
initialize: function() {
document.addEventListener('deviceready', this.onDeviceReady, true);
},
onDeviceReady: function() {
alert(device.uuid);
}
};
app.initialize();
Related
I develop iOS app with Apache Cordova on Xcode. The problem is when I test my app on device, Cordova listens events too late. For example, when I click share button on iOS, the share box runs after 2 minutes. Another example is I use admob pro pluginfor admob, the ads run after 5 minutes on device ready. I recognized that this problem is exist only on Cordova and its plugins events.
I checked and tried everything but couldn't find a solution. On android platform everything is work fine.
How can I fix this? Is there anybody can help me ?
the admob responding after 2-5 minutes. I did type simple javascript function that is alert for device ready. its working fine.
the code snippet is below;
function reklamYukle() {
var admobid = {};
// TODO: replace the following ad units with your own
if (/(android)/i.test(navigator.userAgent)) {
admobid = { // for Android
banner: 'ca-app-pub-5534669392136777/3711456161',
interstitial: 'ca-app-pub-5534669392134577/5454702358'
};
} else if (/(ipod|iphone|ipad)/i.test(navigator.userAgent)) {
admobid = { // for iOS
banner: 'ca-app-pub-5534669392136777/7457497261',
interstitial: 'ca-app-pub-5534669392136777/5896501200'
};
} else {
admobid = { // for Windows Phone
banner: 'ca-app-pub-6869992474017983/8878394753'
};
}
AdMob.createBanner({
adId: admobid.banner,
position: AdMob.AD_POSITION.BOTTOM_CENTER,
overlap: false,
offsetTopBar: true,
bgColor: 'black',
autoshow: true
});
AdMob.prepareInterstitial({
adId: admobid.interstitial,
autoShow: true
});
}
function onDeviceReady() {
reklamYukle();
}
document.addEventListener("deviceready", onDeviceReady, false);
I am trying to get my app to store and restore the screen that it is on, when the app switches to native camera, or to a different app. It seems to work fine for Android, but will not fire for iOS.
initialize: function() {
this.bindEvents();
},
bindEvents: function() {
console.log("Binding Events");
document.addEventListener('deviceready', this.onDeviceReady, false);
document.addEventListener('pause', this.onPause, false);
document.addEventListener('resume', this.onResume, false);
onPause: function() {
// Here, we check to see if we are in the middle of taking a picture. If
// so, we want to save our state so that we can properly retrieve the
// page url in onResume().
window.localStorage.setItem(APP_STORAGE_KEY, window.location.href);
console.log(window.location.href);
},
onResume: function(event) {
// Here we check for stored state and restore it if necessary.
setTimeout(function(){
var storedState = window.localStorage.getItem(APP_STORAGE_KEY);
if(storedState) {
console.log(storedState);
window.location.href=storedState;
}
},0);
},
I want my app to do something when it comes form it's background state to the active state. I understand according the cordova documentation I can do this with the code below and this is working.
// device APIs are available
//
function onDeviceReady() {
document.addEventListener("resume", onResume, false);
}
// Handle the resume event
//
function onResume() {
}
My app is build with Ionic and the downside of using the code above is that it only work outside my app module, so I can't trigger functions inside my app module. I found a few code example on how it should work inside my app module, but non of them are working. See some examples below.
$ionicPlatform.on('resume', function(){
// rock on
});
/
ionic.Platform.ready(function() {
ionic.on('resume', function(){
//rock on
}, element);
});
/
$ionicPlatform.ready(function () {
document.addEventListener("deviceReady", function () {
document.addEventListener("resume", function () {
$timeout(function () {
//rock on
}, 0);
}, false);
});
});
Am I doing something wrong am I forgetting something, I hope someone can help me with this.
Thanks!!
For the ionic you try the following code, hopes it helps to you
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
document.addEventListener("pause", function() {
//code for action on pause
}, false);
document.addEventListener("resume", function() {
//code for action on resume
}, false);
});
We can use the $ionicPlatform to listen the resume event of an ionic application. This event fires once the application resumes.
$ionicPlatform.on("resume", function (event) {
// will execute when device resume.
});
Have a look at CodeExpertz for example
I have the following code:
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
document.addEventListener("backbutton", onBackKeyDown, false);
}
function onBackKeyDown() {
if ($.mobile.activePage.attr("id") === "home") {
e.preventDefault();
navigator.app.exitApp(); //not working
}
}
My onBackKeyDown() function is being entered, now I'm getting a series of strange events:
My if condition is never entered, even when my $.mobile.activePage.attr("id") === "home" is true (tested on weinre server)
navigator.app.exitApp never works, this seems to happen across my whole app, not just this one function.
Back button is unresponsive all over my app.
Any idea why I'm getting this strange behavior? Phonegap 2.6, jquery mobile 1.3.0 and testing on an Android 2.3.7.
In my testing here
if ($.mobile.activePage.attr("id") === "home") {
works perfectly.
Already "navigator.app.exitApp ()" will not really work because "navigator" refers to the browser itself, PhoneGap applications do not use browser but a webview.
But how applications behave differently on each OS version, try the following:
if(navigator.app){
navigator.app.exitApp();
}else if(navigator.device){
navigator.device.exitApp();
}
I'm testing html5 app with cordova and jquery mobile - for Android and Blackberry the following code working for me :
var ua = navigator.userAgent;
window.device = {
iphone: ua.match(/(iPhone|iPod|iPad)/),
blackberry7: ua.match(/91|93|97|96|97|9800|9810|9850|9860|99/),
blackberry10: ua.match(/BB|PlayBook|Tablet/),
android: ua.match(/Android/)
}
//window.device ready for blackberry
if (window.device.blackberry7){
document.addEventListener('deviceready', onDeviceReady, false);
}
//window.device ready for android
else {
window.addEventListener('load', onDeviceReady, false);
}
and then :
function onDeviceReady(){
document.addEventListener("backbutton", onBackKeyDown, false);
document.addEventListener("menubutton", onMenuKeyDown, false);
document.addEventListener("searchbutton", onSearchKeyDown, false);
}
function onBackKeyDown(){
if (window.device.blackberry7){blackberry.app.exit();}
if (!window.device.blackberry7){navigator.app.exitApp();}
}
function onMenuKeyDown(){
if (window.device.blackberry7){alert("BB Menu key");}
if (!window.device.blackberry7){alert("Menu key");}
}
function onSearchKeyDown(){
if (window.device.blackberry7){alert("BB Search key");}
if (!window.device.blackberry7){alert("Search key");}
}
I am developing a Android app using Jquery Mobile/Phonegap. I have the following code to control the phone's back button:
document.addEventListener("backbutton", backKeyDown, true);
function backKeyDown() {
// Call my back key code here.
$.mobile.changePage("#homepage", "slideup");
}
This all works fine, but I would like the app to close when pressing the back button on the homepage only, is this possible?
Update: this has stopped working with a latest Phonegap update (supposedly). Feel free to offer a working solution if you know it.
Here's how I do it:
document.addEventListener("backbutton", function(e){
if($.mobile.activePage.is('#homepage')){
/*
Event preventDefault/stopPropagation not required as adding backbutton
listener itself override the default behaviour. Refer below PhoneGap link.
*/
//e.preventDefault();
navigator.app.exitApp();
}
else {
navigator.app.backHistory()
}
}, false);
For further information, here you can find the related documentation with a full example: http://docs.phonegap.com/en/2.0.0/cordova_events_events.md.html#backbutton
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
document.addEventListener("backbutton", onBackKeyDown, false);
}
function onBackKeyDown()
{
navigator.app.exitApp();
}
Thank you spader.
You would need to keep track of when the homepage is being displayed. When you know you are on the homepage call:
navigator.app.exitApp();
If you don't want to use jQuery Mobile, change $.mobile.activePage.is('#homepage') to document.getElementById('#homepage') on #Spadar Shut answer, as on following code:
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady(){
document.addEventListener("backbutton", function(e){
if(document.getElementById('#homepage')){
e.preventDefault();
navigator.app.exitApp();
}
else {
navigator.app.backHistory()
}
}, false);
}
Through this way, don't need to download Jquery Mobile gibberish only for this purpose. Also, activePage is deprecated as of JQuery mobile 1.4.0 and will be removed from 1.5.0. (Use the getActivePage() method from the pagecontainer widget instead)