Cordova Screen Orientation (Lock) Plugin Not Working

This dev tip is brought to you by the developer of MoneyPhone: Personal Expense Tracking for Android and iOS. Consistently spend less than you earn each month and get your finances under control when you start monitoring your expenses with MoneyPhone!

If you find yourself struggling to get the Apache Cordova screen orientation lock plugin to work, the problem is most likely that you are calling the .lock() method before the ‘deviceready’ event has fired. To fix this problem, simply create a call back function containing your .lock() method call, then pass this callback function as an argument to the deviceready event listener like so =>

First, make sure to install the screen orientation plugin for apache cordova

$ cordova plugin add cordova-plugin-screen-orientation

Then, rewrite your code to look something like this…

// Lock Device Orientation
function lockDeviceOrientationCallback() {
// set to either landscape
screen.orientation.lock('portrait');
console.log("locking device orientation...");
}
document.addEventListener("deviceready", lockDeviceOrientationCallback, false);
 

topherPedersen