Ricky Levi
Ricky Levi

Reputation: 7997

Lock orientation with cordova-plugin-screen-orientation

I'm using the plugin in my Ionic2 application:

cordova-plugin-screen-orientation 2.0.1 "Screen Orientation"

app.component.ts

import { ScreenOrientation } from '@ionic-native/screen-orientation';

export class MyApp {
  constructor(screenOrientation: ScreenOrientation) {
    screenOrientation.lock(screenOrientation.ORIENTATIONS.LANDSCAPE);
  }
}

package.json

"cordova": {
    "plugins": {
      "cordova-plugin-console": {},
      "cordova-plugin-device": {},
      "cordova-plugin-statusbar": {},
      "cordova-plugin-whitelist": {},
      "ionic-plugin-keyboard": {},
      "cordova-plugin-http": {},
      "cordova-plugin-screen-orientation": {}
    },
    "platforms": [
      "android",
      "browser",
      "ios"
    ]
  }

Why the application not working ? error:

NotSupportedError: Operation is not supported

Upvotes: 1

Views: 753

Answers (1)

robbannn
robbannn

Reputation: 5013

You should import Platform from ionic-native and wrap your ScreenOrientation calls in a Platform.ready() call. The ready function returns a Promise that resolves when the platform is ready and native functionality can be called. The reason its not working for you now is probably because the platform is not ready when the function is called.

import { Platform } from 'ionic-angular';
import { ScreenOrientation } from '@ionic-native/screen-orientation';

export class MyApp {
    constructor(private screenOrientation: ScreenOrientation, private platform: Platform) {
        platform.ready().then(() => {
            this.screenOrientation.lock(screenOrientation.ORIENTATIONS.LANDSCAPE);
        });
    }
}

Upvotes: 1

Related Questions