Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

capacitor-screen-orientation

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

capacitor-screen-orientation

Handle screen orientation of a mobile device.

  • 0.0.11
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
61
increased by90.63%
Maintainers
1
Weekly downloads
 
Created
Source

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:

  1. Open “AndroidManifest.xml” for your app
  2. Find the Activity tag
  3. Go to android:configChanges=”...”
  4. Remove ‘orientation |’ from configChanges

Supported Orientations:

  1. LANDSCAPE: left of right is decided by the device’s sensor
  2. LANDSCAPE_PRIMARY: explicitly specified by developer
  3. LANDSCAPE_SECONDARY: explicitly specified by developer
  4. PORTRAIT: up or upside down is decided by the device’s sensor
  5. PORTRAIT_PRIMARY: explicitly specified by the user
  6. PORTRAIT_SECONDARY: explicitly specified by the user
  7. 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:

  1. Open AppDelegate.swift for your app
  2. 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
}
}
}
}

  1. Locate: "func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {"
  2. Add the following code inside the function:
    NotificationCenter.default.addObserver(self, selector: #selector(self.setOrientationLock), name: NSNotification.Name(rawValue: "CAPOrientationLocked"), object: nil)

Supported Orientations:

  1. all: all orientations
  2. allButUpsideDown: all orientations other than upside down
  3. landscape: left of right is decided by the device’s sensor
  4. landscapeLeft: explicitly specified by the user
  5. landscapeRight: explicitly specified by the user
  6. portrait: up or upside down is decided by the device’s sensor
  7. 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:

  1. Create a function to get the current screen orientation:
    async getOrientation() {
    let obj = await ScreenOrientation.getScreenOrientation();
    this.screen_orientation = obj.orientation;
    }

  2. 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:

  1. LANDSCAPE_PRIMARY (Android and iOS)
  2. PORTRAIT_PRIMARY (Android and iOS)
  3. LANDSCAPE_SECONDARY (Android and iOS)
  4. PORTRAIT_SECONDARY (Android and iOS)
  5. LANDSCAPE (Android only)
  6. PORTRAIT (Android only)
  7. CURRENT (Android only)
  1. Create a function to unlock screen rotation:
    async UnlockOrientation() {
    await ScreenOrientation.unlockScreenOrientation({});
    }

  2. 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,
});
}

Keywords

FAQs

Package last updated on 29 Aug 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc