Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Jason_B_3
Beginner
68 Views

Screen Orientation Plugin does not work on CLI 5.4.1 on IOS

This is really a heads up to others who might be trying to get this plugin working.

The workaround is to use CLI 5.1.1 (I have not tested on other platforms, just IOS)
I did test on Early Access, which has the same issues.

I wanted to lock the orientation to only one type of landscape. I was struggling to get this plugin working, and finally I decided to try CLI 5.1.1 and that works

    document.addEventListener("deviceready", onDeviceReady, false);               
    function onDeviceReady(){
      window.screen.lockOrientation('landscape-secondary')
      alert("Current orientation: " + window.screen.orientation);
    }

https://github.com/gbenvenuti/cordova-plugin-screen-orientation

Tags (2)
0 Kudos
3 Replies
PaulF_IntelCorp
Employee
68 Views

Try this code, the API appears to vary as a function of the platform:

app.testToggleOrientation = function() {
    "use strict" ;
    var fName = "toggleOrientation():" ;
    console.log(fName, "entry") ;

    var str = "" ;
    var screenOrientation = "unknown" ;

    if( window.cordova ) {
        try {
            if( cordova.platformId.match(/ios/i) ) {
                screenOrientation = screen.orientation ;
            }
            else if( cordova.platformId.match(/android/i) ) {
                screenOrientation = screen.orientation.type ;
            }
            else {
                screenOrientation = "unknown" ;
            }

            if( screenOrientation.match(/landscape/i) ) {
                screenOrientation = "portrait" ;
                screen.lockOrientation(screenOrientation) ;
            }
            else if( screenOrientation.match(/portrait/i) ) {
                screenOrientation = "landscape" ;
                screen.lockOrientation(screenOrientation) ;
            }
            else {
                screenOrientation = "unlocked" ;
                screen.unlockOrientation() ;
            }

            str = "try succeeded, screen orientation set to: " + screenOrientation ;
            console.log(fName, str) ;
        }
        catch(e) {
            str = "try failed: " + e ;
            console.log(fName, str) ;
            alert(str) ;
        }
    }

    console.log(fName, "exit") ;
} ;



app.windowEventOrientationChange = function() {
    "use strict" ;
    var fName = "windowEventOrientationChange():" ;

    var str = "" ;
    var screenOrientation = "unknown" ;

    if( window.cordova ) {
        if( cordova.platformId.match(/ios/i) )
            screenOrientation = screen.orientation ;
        else if( cordova.platformId.match(/android/i) )
            screenOrientation = screen.orientation.type ;
    }

    str = "Screen orientation is: " + screenOrientation ;
    console.log(fName, str) ;
} ;
window.addEventListener("orientationchange", app.windowEventOrientationChange) ;

 

Jason_B_3
Beginner
68 Views

Thanks very much for taking the time Paul.  I was not aware we could access it like this.

Jason

PaulF_IntelCorp
Employee
68 Views

I only ran into it because I created a demo app using it and discovered this problem, couldn't find anything in the plugin docs, but figured it out by looking at the plugin code.

Reply