@capacitor/screen-orientation plugin breaks app on iPad. Can it be told to load only for Android and iOS (but not iPadOS)?

Hello.

This is my first app using Capacitor, so please be gentle :grin: .

I am using the @capacitor/screen-orientation plugin to force my app into portrait mode. This works great on Android devices and iPhones (iOS). Sadly, after hours of debugging, I discovered that it was this plugin that breaks the app on iPads. This is due to Apple apparently having removed the API to force orientation on iPadOS. I had no idea and it would appear most of the internet doesn’t either. This causes the app to fail running any JavaScript after it opens.

Having the plugin present doesn’t seem to cause an issue until you come to use it so I can live with it being installed, but it would be nice to have conditions on these two JavaScript lines so they are not run on iPads:

import { ScreenOrientation } from ‘@capacitor/screen-orientation’;
await ScreenOrientation.lock({orientation: “portrait”});

Is it possible to do this? Have device/OS conditions inside the JavaScript? I am currently using Capacitor 7.4.2 if that makes any difference.

Thank you for any help or advice you can provide.

If you are using Ionic, you can use the isPlatform method.

If not, you can use Capacitor’s Device plugin and getInfo method. Not sure how easy it would be to differentiate between an iPhone and and iPad though.

You might also want to try Capawesome’s plugin - Screen Orientation Plugin for Capacitor - Capawesome. It mentions special iPad handling.

1 Like

Thank you twestrick! You are a star.

I replaced the plugin with the Capawesome Screen Orientation one and it works perfectly on Android, iPhone and iPad. For anyone else reading this, don’t forget to follow their instructions to amend the AppDelegate.swift and Info.plist files :+1:

1 Like