@wisdomgarden/capacitor-screen-orientation
v0.0.4-4
Published
Handle screen orientation of a mobile device.
Downloads
9
Readme
Ionic Capacitor Screen Orientation
For detailed tutorial on how to use this plugin visit: https://medium.com/@hinddeep.purohit007/handling-screen-orientation-changes-in-capacitor-apps-19fe339578a6
Demo Application: https://github.com/hinddeep/Demo-Ionic-Screen_Orientation
Platforms Supported: Android, iOS
The Capacitor plugin I’ve developed can be used to detect the current of orientation of the screen, lock the screen in a particular orientation (disable auto-rotate) or unlock screen rotation (enable auto-rotate) and to listen for orientation changes.
Installation
npm install capacitor-screen-orientation
Android Configuration:
Open MainActivity.java and add the following code inside this.init() add(ScreenOrientation.class); Adding the above mentioned line will add the following import statement: import com.bkon.capacitor.screenorientation.ScreenOrientation; If you encounter errors, please add both the lines manually to MainActivity.java
If you want to listen for the orientation change event on Android:
- Open “AndroidManifest.xml” for your app
- Find the Activity tag
- Go to android:configChanges=”...”
- Remove ‘orientation |’ from configChanges
Supported Orientations:
- LANDSCAPE: left of right is decided by the device’s sensor
- LANDSCAPE_PRIMARY: explicitly specified by developer
- LANDSCAPE_SECONDARY: explicitly specified by developer
- PORTRAIT: up or upside down is decided by the device’s sensor
- PORTRAIT_PRIMARY: explicitly specified by the user
- PORTRAIT_SECONDARY: explicitly specified by the user
- CURRENT: current orientation of the device
SPECIAL NOTE: Ionic has implicitly disabled PORTRAIT_SECONDARY.
iOS Configuration:
If you want to lock the screen to the specified orientation on iOS:
- Open AppDelegate.swift for your app
- Add the following code:
var orientationLock = UIInterfaceOrientationMask.all
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { return self.orientationLock }
@objc func setOrientationLock(_ notification: Notification) { if let data = notification.userInfo as? [String: Int] { for (_, mask) in data { switch mask { case 1: self.orientationLock = UIInterfaceOrientationMask.portrait break; case 2: self.orientationLock = UIInterfaceOrientationMask.portraitUpsideDown break; case 3: self.orientationLock = UIInterfaceOrientationMask.landscapeRight break; case 4: self.orientationLock = UIInterfaceOrientationMask.landscapeLeft break; case 5: self.orientationLock = UIInterfaceOrientationMask.landscape break; default: self.orientationLock = UIInterfaceOrientationMask.all } } } }
- Locate: "func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {"
- Add the following code inside the function: NotificationCenter.default.addObserver(self, selector: #selector(self.setOrientationLock), name: NSNotification.Name(rawValue: "CAPOrientationLocked"), object: nil)
Supported Orientations:
- all: all orientations
- allButUpsideDown: all orientations other than upside down
- landscape: left of right is decided by the device’s sensor
- landscapeLeft: explicitly specified by the user
- landscapeRight: explicitly specified by the user
- portrait: up or upside down is decided by the device’s sensor
- portraitUpsideDown: explicitly specified by the user
SPECIAL NOTE: Ionic has implicitly disabled portraitUpsideDown.
Import the Plugin in your Web app
Open app.component.ts file and import the plugin as follows: import { Plugins } from "@capacitor/core"; const { ScreenOrientation } = Plugins; import 'capacitor-screen-orientation'
SPECIAL NOTE: Remove import 'capacitor-screen-orientation' when compiling app for Android and iOS. THe native plugin will not be invoked if you forget to remove the import statement before building for Android and iOS Platform.
Handle screen orientation:
Create a function to get the current screen orientation: async getOrientation() { let obj = await ScreenOrientation.getScreenOrientation(); this.screen_orientation = obj.orientation; }
Create a function to lock the screen in a particular orientation: async lockOrientation() { await ScreenOrientation.lockScreenOrientation({ orientation: this.screen_orientation_lock, }); }
Supported values for screen_orientation_lock variable:
- LANDSCAPE_PRIMARY (Android and iOS)
- PORTRAIT_PRIMARY (Android and iOS)
- LANDSCAPE_SECONDARY (Android and iOS)
- PORTRAIT_SECONDARY (Android and iOS)
- LANDSCAPE (Android only)
- PORTRAIT (Android only)
- CURRENT (Android only)
Create a function to unlock screen rotation: async UnlockOrientation() { await ScreenOrientation.unlockScreenOrientation({}); }
Create a function that handles subscription to the orientation change event: subscribeToOrientationChanges() { ScreenOrientation.addListener("orientation_changed", (data) => { this.screen_orientation_event = data.orientation; }); }
Rotate to a specific orientation (without locking the orientation):
NOTE: NOT supported on Android async rotate() { await ScreenOrientation.rotateTo({ orientation: this.rotateTo, }); }