Git Product home page Git Product logo

jspermissionhandler's Introduction

This is a MAUI Blazor library to simplify permission management for Blazor Javascript APIs like camera, microphone (through getUserMedia) or geolocation. This is accomplished hugely thanks to MackinnonBuck/MauiBlazorPermissionsExample. It worked for many of my projects so it's time to pack it to easily reuse it.

This library is highly customizable and extensible. Most methods are virtual and can be overriden to fit your needs.

See a full demo project at project Github.

This package does not support Tizen.

Installation

Install the NuGet package in your project:

dotnet add package JsPermissionHandler

Setup Permissions

Windows

You do not need to do anything special to use this library on Windows.

Android

Add permissions to your AndroidManifest.xml file (in Platforms/Android).

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Note: only add what you need. For getUserMedia with audio, you need both RECORD_AUDIO and MODIFY_AUDIO_SETTINGS.

iOS

Add permissions to your Info.plist file (in Platforms/iOS).

<key>NSLocationWhenInUseUsageDescription</key>
<string>This app requires access to your location. Please grant access to your precise location when requested.</string>
<key>NSCameraUsageDescription</key>
<string>This app requires access to your camera. Please grant access to your camera when requested.</string>
<key>NSMicrophoneUsageDescription</key>
<string>This app requires access to your microphone. Please grant access to your microphone when requested.</string>

Add the Handler to your WebView

In your MainPage.xaml.cs file, add the following code:

public MainPage()
{
    InitializeComponent();

    new BlazorWebViewHandler()            
        // Add whichever you need:
        .AddCamera()
        .AddMicrophone()
        .AddGeolocation()
        // blazorWebView is the name of your BlazorWebView
        .AddInitializingHandler(blazorWebView);
}

And that's it, you can now use your Javascript APIs in the Blazor Webview.

Additional permissions

The default BlazorWebViewHandler gives the three common permissions call AddCamera, AddMicrophone and AddGeolocation. If you need more, you need to inherit PermissionHandler and add your own. This is only needed for Android. For example:

// MyHandler.Android.cs

public class MyHandler : PermissionHandler
{
	public MyHandler AddMyPermission() =>
        AddWebkitPermission(
            [
                (Manifest.Permission.ModifyAudioSettings, null),
                (Manifest.Permission.RecordAudio, rationale),
            ],
            PermissionRequest.ResourceAudioCapture
        );
}

Opening Permission panel

Usually working with permissions flow requires to open the permission panel especially when user denies your app access to permission. This is done with the PermissionHandler.OpenAppPermissionPanelAsync static method.

public static partial Task OpenAppPermissionPanelAsync(string? windowsScheme = null);
  • windowsScheme is used on Windows only. You can specify which Windows Settings panel to open. If null, the default is ms-settings:appsfeatures-app to open the settings of the current app (which would include permissions). Windows Permission
  • On Android, this method opens the app settings panel. It's not possible to open the permission panel directly.
  • On iOS, this method opens the app settings panel which includes the permissions.

jspermissionhandler's People

Contributors

datvm avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

jspermissionhandler's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.