I have to wrap ScreenOrientation and Device (for AndroidPermissions) into the deviceready event. But deviceready is never fired.
Apart from the deviceready event, my app works as expected. And I use the native function BarcodeScanner and have several views that consume a REST API.
My “not working” code at first.
app.component.ts
import { Device } from '@ionic-native/device/ngx';
import { ScreenOrientation } from '@ionic-native/screen-orientation/ngx';
import { AndroidPermissions } from '@ionic-native/android-permissions';
constructor(private device: Device, private screenOrientation: ScreenOrientation, private androidPermissions: AndroidPermissions) {
document.addEventListener('deviceready', this.onDeviceReady, false);
}
onDeviceReady() {
console.log('device is ready');
this.screenOrientation.lock(this.screenOrientation.ORIENTATIONS.PORTRAIT_PRIMARY);
if (this.device.platform == 'Android') {
console.log(this.device.platform);
this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.INTERNET).then(
result => console.log('Has permission?', result.hasPermission),
error => this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.INTERNET)
);
}
}
The function onDeviceReady()
is never called.
ionic info
Ionic:
ionic (Ionic CLI) : 4.10.1 (/usr/lib/node_modules/ionic)
Ionic Framework : ionic-angular 3.9.2
@ionic/app-scripts : 3.2.0
Cordova:
cordova (Cordova CLI) : 8.1.2 (cordova-lib@8.1.1)
Cordova Platforms : android 7.1.1, browser 5.0.4
Cordova Plugins : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 2.1.4, (and 9 other plugins)
System:
Android SDK Tools : 26.1.1 (/opt/google/android/sdk)
NodeJS : v8.15.0 (/usr/bin/node)
npm : 6.7.0
OS : Linux 4.15
How do you implement it correctly?
Why is deviceready not fired with my code?