I was using the cordova-plugin-screen-orientation package previously for locking certain pages of my ionic/angular/capacitor app in landscape mode. That seems to have stopped working with a recent iOS update (I’m running a device with 16.4.1).
I’m trying to use the @capacitor /screen-orientation package, but when I call:
await ScreenOrientation.lock({ orientation: "landscape" })
all it seems to do is lock my CURRENT screen orientation and doesn’t actually force the device into landscape and lock it (both iOS and Android). Am I misunderstanding the intention here or am I just doing something insanely dumb (guessing it’s both!)?
“@capacitor /screen-orientation”: “^4.1.0”
It’s a known bug on the plugin that has been fixed, but not released yet (unless you want to use capacitor 5 beta and the beta version of the plugin)
opened 11:39AM - 03 Mar 23 UTC
closed 01:45PM - 31 Mar 23 UTC
platform: android
platform: ios
type: bug
plugin: screen-orientation
## Bug Report
### Plugin(s)
<!--
List the plugins and versions that this bu… g affects.
-->
@capacitor/screen-orientation@4.1.0
### Capacitor Version
<!--
Paste the output from the `npx cap doctor` command into the code block below. This will provide the versions of Capacitor packages and related dependencies.
-->
```
💊 Capacitor Doctor 💊
Latest Dependencies:
@capacitor/cli: 4.7.0
@capacitor/core: 4.7.0
@capacitor/android: 4.7.0
@capacitor/ios: 4.7.0
Installed Dependencies:
@capacitor/core: 4.7.0
@capacitor/cli: 4.7.0
@capacitor/ios: 4.7.0
@capacitor/android: 4.7.0
```
### Platform(s)
<!--
List the platforms that this bug affects.
-->
- iOS
- Android
### Current Behavior
<!--
Describe how the bug manifests. Be specific.
-->
Calling
```typescript
ScreenOrientation.lock({orientation: 'landscape'})
```
locks the screen orientation to `portrait-primary`.
### Expected Behavior
<!--
Describe what the behavior should be.
-->
Calling
```typescript
ScreenOrientation.lock({orientation: 'landscape'})
```
should lock the screen orientation to either `landscape-primary` or `landscape-secondary`.
### Code Reproduction
<!--
To isolate the cause of the problem, we ask you to provide a minimal sample application that demonstrates the issue.
For full instructions, see: https://github.com/ionic-team/capacitor/blob/master/CONTRIBUTING.md#creating-a-code-reproduction
-->
### Other Technical Details
<!--
Please provide the following information with your request and any other relevant technical details (versions of IDEs, local environment info, plugin information or links, etc).
-->
### Additional Context
<!--
List any other information that is relevant to your issue. Stack traces, related issues, suggestions on how to fix, Stack Overflow links, forum links, etc.
-->
The [ScreenOrientation.lock() specification](https://developer.mozilla.org/en-US/docs/Web/API/ScreenOrientation/lock) and the typescript type `OrientationLockType` allow the values
```typescript
"any" | "landscape" | "landscape-primary" | "landscape-secondary" | "natural" | "portrait" | "portrait-primary" | "portrait-secondary"
```
The current Android and iOS implementations of `@capacitor/screen-orientation@4.1.0` only handle `landscape-primary`, `landscape-secondary`, `portrait-primary` and `portrait-secondary`. In all other cases they fall back to `portrait-primary`:
```java
private int fromOrientationTypeToEnum(String orientationType) {
switch (orientationType) {
case "landscape-primary":
return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
case "landscape-secondary":
return ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
case "portrait-secondary":
return ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
default:
// Case: portrait-primary
return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
}
}
```
```swift
private func fromOrientationTypeToMask(_ orientationType: String) -> UIInterfaceOrientationMask {
switch orientationType {
case "landscape-primary":
return UIInterfaceOrientationMask.landscapeLeft
case "landscape-secondary":
return UIInterfaceOrientationMask.landscapeRight
case "portrait-secondary":
return UIInterfaceOrientationMask.portraitUpsideDown
default:
// Case: portrait-primary
return UIInterfaceOrientationMask.portrait
}
}
```
A `landscape` case should be added that locks the screen orientation to one of the landscape orientations.
You can use landscape-primary
or landscape-secondary
, but not landscape
until the fix is released.
Thanks much! I clearly missed the bug but just confirmed that the workaround does solve my problem. Thanks again