How to implement Capacitor plugins?

Sorry if this is a dumb question but…

How does a beginner learn how to use Capcitor plugins? For example, “Screen Orientation”. I found this page: Screen Orientation Capacitor Plugin API | Capacitor Documentation

This page has the following intall instructions:

npm install @capacitor/screen-orientation
npx cap sync

Thats helpful, but there doesn’t seem to be any code samples, or info on how to import or use this plugin in an app.

Am I missing something? Is there some documentation or sample code somewhere? In general, how does one go about figuring out how to get these plugins to work?

thanks…

The same page has a lot of information about native adjustments and the API when you scroll down - did you see that?

Thanks for the response.

Yes, I looked over the entire page, but found no usefull info on how to set the plugin up or use it. For example, from the page:

orientation()
orientation() => Promise

Returns the current screen orientation.

Returns: Promise

Since: 4.0.0

But how do you import the plugin? How do you call this function? I’ve looked at serveral examples I’ve found via google, and looking at the code they seem very straight forward, but none of them work for me (for example import {ScreenOrientation} from … causes errors ) . The examples obviously worked in the past… do the plugins change to where you have to install/configure them differently now? If so is that documented somewhere?

I’ve managed to get several plugins to work (splashscreen, http, and sqlite) but each one seems to be an epic struggle to get working…

Hey again,

Capacitor Plugins work by direct usage, means:

  1. Import
  2. Call

So similar to the Splash Screen plugin where it is

import { SplashScreen } from '@capacitor/splash-screen';
...
SplashScreen.hide();

it should be for screen orientation:

import { ScreenOrientation } from '@capacitor/screen-orientation';
...
ScreenOrientation.orientation()

What error do you have with this?

Adding an new plugin should not be struggle. There are plugins that require more setup in native projects than others, but at the end of the day it always should be quire easy to add & use.

1 Like