When testing locally on Chrome using ionic server, the following code produces an error:
ScreenOrientation.lockOrientation('portrait');
The error is:
Uncaught (in promise): TypeError: Cannot read property 'apply' of undefined
The code needs to be surrounded by a try/catch in order to make the app work
$ ionic info
Your system information:
Cordova CLI: 6.4.0
Ionic Framework Version: 2.0.0-rc.3
Ionic CLI Version: 2.1.8
Ionic App Lib Version: 2.1.4
Ionic App Scripts Version: 0.0.45
ios-deploy version: 1.9.0
ios-sim version: 5.0.11
OS: OS X El Capitan
Node Version: v4.2.3
Xcode version: Xcode 8.1 Build version 8B62
Can you run ionic info in your project folder.
@ihadeed updated the description
I also have this error, not only with ionic serve, but also when running on Android.
Here is what error says on Android :
`TypeError: Cannot read property 'apply' of undefined
at callCordovaPlugin (file:///android_asset/www/build/js/app.bundle.js:93457:63)
at Function.<anonymous> (file:///android_asset/www/build/js/app.bundle.js:93597:20)
at Function.value [as lockOrientation] (file:///android_asset/www/build/js/app.bundle.js:93660:61)`
ionic info
Your system information:
Cordova CLI: 6.3.1
Ionic Framework Version: 2.0.0-beta.11
Ionic CLI Version: 2.1.0
Ionic App Lib Version: 2.1.0-beta.1
OS:
Node Version: v4.4.7
(OS is actually windows 10 and I use crosswalk plugin)
@mebibou this would be the same issue here: https://github.com/driftyco/ionic-native/issues/814#issuecomment-263903300
ScreenOrientation plugin uses window.screen which is defined in every browser, but the plugin specific properties are only available in Cordova environment.
@bnoeson Crosswalk fires the deviceready even before it's ready, wrap your code with setTimeout and give it ~100ms and it should work (increase the timeout if needed).
@ihadeed yes you told me to open an issue for it here
I have the same issue. It fails even if you just import the plugin using import { ScreenOrientation } from 'ionic-native';
When the app gets the page with import it fails with error.
It fails only in browser. I can't even use conditions like platform.is('core') to not use the plugin in browser, but it doesn't help, because page fails immediately.
Please advice.
Just pushed a commit that should fix this. I wrapped the .apply call with try/catch so it can handle cases like this. The fix will be available in the next release.
subscribing to this. does the try/catch block mean that we won't be able to rely on this to lock the orientation? if so, what are the roadblocks to finding a long term solution?
@jonathan-chin I got rid of the try/catch block. I fixed this issue in another way in a different commit.
The issue here was caused on browser (plugin doesn't work on browser), or on the device because they were called before deviceready event fired.
@ihadeed I am still facing that problem. Using ionic-native 2.5.1 .
ordova CLI: 6.5.0
Ionic Framework Version: 2.0.1
Ionic CLI Version: 2.2.1
Ionic App Lib Version: 2.2.0
Ionic App Scripts Version: 1.1.0
ios-deploy version: Not installed
ios-sim version: Not installed
OS: Windows 10
Node Version: v7.5.0
Xcode version: Not installed
Here is my code:
setTimeout(ScreenOrientation.lockOrientation("landscape"),500);
Thank you!
@supryin, you need to build browser platform and run ionic-serve pointing it's build output see this issue
Most helpful comment
@ihadeed I am still facing that problem. Using ionic-native 2.5.1 .
ordova CLI: 6.5.0
Ionic Framework Version: 2.0.1
Ionic CLI Version: 2.2.1
Ionic App Lib Version: 2.2.0
Ionic App Scripts Version: 1.1.0
ios-deploy version: Not installed
ios-sim version: Not installed
OS: Windows 10
Node Version: v7.5.0
Xcode version: Not installed
Here is my code:
setTimeout(ScreenOrientation.lockOrientation("landscape"),500);
Thank you!