5

I strangle to be able to catch this error. While on desktop, this code raise this NotSupportedError:

enter image description here

I normally debug on chrome.

Here is the code:

import {Component} from "@angular/core";
import {ScreenOrientation} from "@ionic-native/screen-orientation";

@IonicPage()
@Component({
  selector: 'page-loading',
  templateUrl: 'page-loading.html',
})
export class PageLoading {

  constructor(private screenOrientation:ScreenOrientation) {}

  ionViewDidLoad() {
    console.log('ionViewDidLoad PageLoading');

    try{
        this.screenOrientation.lock(this.screenOrientation.ORIENTATIONS.PORTRAIT).then(()=>{
          console.log('lock');
        });
    }catch(e){
      console.warn('No cordova.js')
    }

  }

}
8
  • ionic native or rather cordova does not work with ionic serve.. you just have to run in a device or emulator Commented May 15, 2017 at 11:48
  • Yes, I know, but for other plugins that is not a problem, they complain silency. I just want to silence this error on my desktop, but the try catch does not help Commented May 15, 2017 at 12:04
  • oh you mean in the ionic view screen? Commented May 15, 2017 at 12:04
  • No I use ionic serve like you said, I just don't know why I can catch these errors Commented May 15, 2017 at 12:06
  • ionicframework.com/docs/native/browser.html Commented May 15, 2017 at 12:07

1 Answer 1

6

You can create a class to mock ionic native classes as described in the docs here.

class ScreenOrientationMock extends ScreenOrientation {
  lock(type) {
    return new Promise((resolve, reject) => {
      resolve("locked");
    })
  }
}

In your providers list in ngModule mention it should use your mocked class instead of the actual ionic native one.

providers: [..
    { provide: ScreenOrientation, useClass: ScreenOrientationMock }
 ]

This will return whatever you have set in resolve for screenorientation during ionic serve.

You can remove it once it is run in a device.

EDIT:

There is another possibility to suppress your error, so you will not have anything to do at the end:

if(this.platform.is('cordova')){
  this.platform.ready().then(()=>{
   this.screenOrientation.lock(this.screenOrientation.ORIENTATIONS.PORTRAIT);
  })
}
Sign up to request clarification or add additional context in comments.

3 Comments

remove it once it is run in a device. you mean that we have to manually change the code later to provide real classes instead of mocks when deploying the app? Or is there a way to detect the device you are running and provide the mock or not automatically?
Hi, no in fact you should not use any mock at the end. I update the response
@DavidF. thanks.. you could have added as your own answer though

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.