Cordova deviceready not firing in iOS - ios

I am using Ionic to build my hybrid app.
On Android my app is running quite fine. Now I was building it for iOS but my entire content that is wrapped inside
document.addEventListener('deviceready', function () {
console.log('dashboard -> device ready');
// copy database to the device
DbAccess.copyDb();
}, false);
is not fired in iOS.
I tried it then with the iOS simulator
$ ionic emulate ios --livereload
and checked the console.log.
Result:
1 709252 log deviceready has not fired after 5 seconds.
2 709253 log Channel not fired: onCordovaInfoReady
I followed the suggestions I found here and via Google but unfortunately without any success (removing ios and re-adding, etc)
My versions:
Cordova: 6.1.1
Ionic: 1.7.14
My Security setup in my index.html:
<meta http-equiv="Content-Security-Policy" content="default-src *; script-src 'self' 'unsafe-inline' 'unsafe-eval' *; style-src 'self' 'unsafe-inline' *">
Inside my config.xml:
<access origin="geo:*" launch-external="yes"/>
<access origin="tel:*" launch-external="yes"/>
<access origin="mailto:*" launch-external="yes"/>
I really have no idea where I could go on to solve that bug. Maybe it is something with the Content-Security-Policy but I have no idea how to fix that.
Thanks in advance for your help!
Edit:
I wrapped my controller(s) now inside
$ionicPlatform.ready(function() {...});
e.g.:
var dashboardCtrl = function ($scope, $ionicPlatform, $cordovaDevice, DbAccess) {
console.log('dashboardCtrl');
$ionicPlatform.ready(function () {
console.log('dashboard -> platform ready');
document.addEventListener('deviceready', function () {
console.log('dashboard -> device ready');
// copy database to the device
DbAccess.copyDb();
}, false);
});
};
dashboardCtrl.$inject = ['$scope', '$ionicPlatform', '$cordovaDevice', 'DbAccess'];
It still fails and throws the "deviceready" error in the console.log while I am testing it with the iOS simulator.

If you are using Ionic why you are manually using document.addEventListener('deviceready', function () the $ionicPlatform.ready give you the same result as the 'deviceready' of the cordova, no need to call deviceready again.
Another thing, you should not put the ready function in custom function of the controller. should not use like this
$scope.logInSubmit = function (details) {}
or
var submit = function () {}
If you check the sample of Ionic you will get better idea of both the items (ex: ionic start myApp tabs)
In Ionic your code can be like this
$ionicPlatform.ready(function() {
console.log('dashboard -> device ready');
//you can load your plugins or custom objects inside this device ready
}

I catch the event with
$ionicPlatform.ready(function () {
...
})
It works on a real device, I had never test on emulator.
I don't think that Content-Security-Policy is the reason that you don't get the event.

Related

cordova barcode scanner plugin not loading in iOS after granting camera access

I'm using the cordova barcode scanner plugin for my hybrid iOS mobile app. The issue I'm having is on initial use of the scanner, if the user has not granted camera permission it prompts for camera access (as expected). However, it does not bring up the camera or execute any of the callbacks. If you close the app and re-try it will work correctly since it no longer requires a permission check.
I have no experience with Objective C but I've been attempting to narrow down the cause using breakpoints in the following file:
https://github.com/phonegap/phonegap-plugin-barcodescanner/blob/master/src/ios/CDVBarcodeScanner.mm
LN511 - Adding a breakpoint here is where the prompt appears asking for camera permission. If I then accept the permission while this breakpoint is active and resume execution everything works correctly and the camera UI opens. If I instead continue all execution (it then calls scanBarcode()) until its complete and then accept permission the camera does not open and no callbacks are executed.
Has anyone experienced this or have any advice on how I can resolve this issue?
EDIT: I also tried the suggested content security policy but it did not resolve the issue:
<meta http-equiv="Content-Security-Policy" content="default-src * gap://ready file:;default-src * 'unsafe-eval'; style-src 'self' 'unsafe-inline'; script-src * 'self' 'unsafe-inline' 'unsafe-eval'" />
EDIT2:
Shouldn't the Obj C code be waiting for camera access to be granted at some point before proceeding? I can't seem to find it doing that anywhere. Once again I don't know Obj. C so I'm kinda researching as I go, but this sample below (from another SO question) requests access and has a completionHandler.
[AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {
if(granted){
NSLog(#"Granted access to %#", mediaType);
} else {
NSLog(#"Not granted access to %#", mediaType);
}
EDIT3 : JS Code
`const scannerOpts = {
preferFrontCamera: false, // iOS and Android
showFlipCameraButton: false, // iOS and Android
showTorchButton: true, // iOS and Android
torchOn: false, // Android, launch with the torch switched on (if available)
prompt: "Place a barcode inside the scan area", // Android
resultDisplayDuration: 500, // Android, display scanned text for X ms. 0 suppresses it entirely, default 1500
//formats : "QR_CODE,PDF_417", // default: all but PDF_417 and RSS_EXPANDED
//orientation : "landscape", // Android only (portrait|landscape), default unset so it rotates with the device
disableAnimations: true, // iOS
disableSuccessBeep: false // iOS
};
cordova.plugins.barcodeScanner.scan(
function(scanResult) {
debugger;
resolve(scanResult);
},
function(scanError) {
debugger;
reject(scanError);
},
scannerOpts
);`
Thanks!

Device Ready ios not working

I know this is repeated like ton of times but I cannot find the way to resolve it.
We have an app running in android with no problems.
But when I install it in my iphone the device ready does not get fired.
The device ready jumps in android but in ios if auto-hide-splash-screen is false it keeps stuck in the splashscreen and deviceready is not fired.
I am making the build with phonegap build.
The version of cordova is 3.7.0
the code of the device ready is
document.addEventListener('deviceready', function() {
alert('listening to device ready'+navigator);
navigator.splashscreen.hide();
}, false);
Try something like :
var onLoad = function(){
document.addEventListener("deviceready", deviceReady, false);
};
var deviceReady = function(){
document.addEventListener("backbutton", exitCordova, false);
alert('listening to device ready'+navigator);
navigator.splashscreen.hide();
};
var exitCordova = function(){
navigator.app.exitApp();
};

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-Blackberry backpress issue/how to handle back key in phonegap

when i pressed back key using blackberry simulator then its close my whole application but when i pressed back button which i provided in header then its working fine.how to solved this is? is there any backpress handing in phonegap? Same code for android works fine means it doesn't close application on back key press.
Or
<script type="text/javascript">
// Wait for PhoneGap to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap is ready
//
function onDeviceReady() {
blackberry.system.event.onHardwareKey(blackberry.system.event.KEY_BACK,
function() {
history.back();
return false;
});
}
</script>
On config.xml --> <feature id="blackberry.system.event" version="1.0.0"/>

Showing splash screen in PhoneGap/Cordova 1.5.0

I'm trying to get my splash screen to show for my PhoneGap/Cordova native iOS app. The default or boolean for this behavior is set to 'YES'. I'm trying to get the screen to show for 2 seconds. At the moment, it only shows for a split second and then goes right into the app. Since my app does not download any data for the web on start, it loads pretty quickly.
According to this advice, this solution works for some. I'm using the latest Cordova 1.5.0 build: http://shazronatadobe.wordpress.com/2011/09/15/ios-phonegap-splash-screen-control/
I've turned the AutoHideSplashScreen to 'NO' in the Cordova.plist and I'm using this code:
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
setTimeout(function() {
navigator.splashscreen.hide();
}, 2000);
I've tried putting the setTimeout function within a function, but to no avail. Like so:
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
function hideSplash() {
setTimeout(function() {
navigator.splashscreen.hide();
}, 2000);
}
And then loading the 'hideSplash()' when the Body loads. But to no avail.
Anything wrong with my code?
OK, I took RespectTheCode's advice and removed the device ready event out of the onLoad function. This is the working code. I'm putting the full code for other's who are learning:
<script>
document.addEventListener("deviceready", onDeviceReady, false);
setTimeout(function() {
navigator.splashscreen.hide();
}, 1000);
</script>
The 1000 is one second for those who don't know. Will not work for less than one second i.e 500. Splash screen just hangs.
This is working for me when I install and app works fine,however, when I restart the device (iPad2) the splash screen hangs like the previous problem (above paragraph) and will not enter the app. Weird bug.
Anyone else experience this?
I'm on iOS 5.1 building with Xcode 4.3.1 and Cordova 1.5.0
I am using Cordova 2.1.0 and now 2.2.0.
When calling navigator.splashscreen.hide(), the Splash Screen didn't disappear.
Using the following code, it worked:
private static native void hideSplashScreen() /*-{
$wnd.Cordova.exec(null, null, "SplashScreen", "hide", []);
}-*/;
Hope this helps

Resources