JsPermissionHandler 1.0.0

dotnet add package JsPermissionHandler --version 1.0.0
NuGet\Install-Package JsPermissionHandler -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="JsPermissionHandler" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add JsPermissionHandler --version 1.0.0
#r "nuget: JsPermissionHandler, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install JsPermissionHandler as a Cake Addin
#addin nuget:?package=JsPermissionHandler&version=1.0.0

// Install JsPermissionHandler as a Cake Tool
#tool nuget:?package=JsPermissionHandler&version=1.0.0

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/JsPermissionHandlersExample. 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 JsPermissionHandlers

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.
Product Compatible and additional computed target framework versions.
.NET net8.0-android34.0 is compatible.  net8.0-ios17.0 is compatible.  net8.0-maccatalyst17.0 is compatible.  net8.0-windows10.0.19041 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 570 11/19/2023