Xamarin.Forms WebView not working with WebRTC

Tags: , , ,



I have worked on WebRTC project, it all works on Web Browser but when I put in in Application WebView, it said not authorized to access device kind a message.

I read other solution on granting access on permission request, it seems no luck for me yet; still cannot access the camera/mic

Here’s some of my implementations (C# language):

...
Control.SetWebChromeClient(new WebChromeClient(context))
...
//on WebChromeClient class
public override void OnPermissionRequest(PermissionRequest request)
{        
   mContext.RunOnUiThread(() =>
   {
       request.Grant(request.GetResources());

   });
}

The same implementations done in Java Android, the same results not authorized. Tested device is on Android 9 & Android 10.

Other android permissions used:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAPTURE_AUDIO_OUTPUT" />
<uses-permission android:name="android.permission.CAPTURE_SECURE_VIDEO_OUTPUT" />
<uses-permission android:name="android.permission.CAPTURE_VIDEO_OUTPUT" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

Any implementation on Java or C# acceptable. Hope you can give some suggestions.

Editted

As per @WendyZang-MSFT suggestion using PermissionPlugin, here’s the code below.

//The Utils class
    public static class Utils
    {
        public static async Task<PermissionStatus> CheckPermissions(Permission permission)
        {
            var permissionStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(permission);
            bool request = false;
            if (permissionStatus == PermissionStatus.Denied)
            {
                if (Device.RuntimePlatform == Device.iOS)
                {

                    var title = $"{permission} Permission";
                    var question = $"To use this plugin the {permission} permission is required. Please go into Settings and turn on {permission} for the app.";
                    var positive = "Settings";
                    var negative = "Maybe Later";
                    var task = Application.Current?.MainPage?.DisplayAlert(title, question, positive, negative);
                    if (task == null)
                        return permissionStatus;

                    var result = await task;
                    if (result)
                    {
                        CrossPermissions.Current.OpenAppSettings();
                    }

                    return permissionStatus;
                }

                request = true;

            }

            if (request || permissionStatus != PermissionStatus.Granted)
            {
                var newStatus = await CrossPermissions.Current.RequestPermissionsAsync(permission);

                if (!newStatus.ContainsKey(permission))
                {
                    return permissionStatus;
                }

                permissionStatus = newStatus[permission];

                if (newStatus[permission] != PermissionStatus.Granted)
                {
                    permissionStatus = newStatus[permission];
                    var title = $"{permission} Permission";
                    var question = $"To use the plugin the {permission} permission is required.";
                    var positive = "Settings";
                    var negative = "Maybe Later";
                    var task = Application.Current?.MainPage?.DisplayAlert(title, question, positive, negative);
                    if (task == null)
                        return permissionStatus;

                    var result = await task;
                    if (result)
                    {
                        CrossPermissions.Current.OpenAppSettings();
                    }
                    return permissionStatus;
                }
            }

            return permissionStatus;
        }
    }

This is my runtime permission:

        protected override void OnAppearing()
        {
            base.OnAppearing();
            RunTimePermission();
        }
        public async void RunTimePermission()
        {
            var status = PermissionStatus.Unknown;

            status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Camera);

            if (status != PermissionStatus.Granted)
            {

                status = await Utils.CheckPermissions(Permission.Camera);
                await Utils.CheckPermissions(Permission.Microphone);

            }

            status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Microphone);
            if (status != PermissionStatus.Granted)
            {
                status = await Utils.CheckPermissions(Permission.Microphone);

            }
        }

Running on PermissionPlugin seems good as it asking permission on web loaded, still got error said:

Requires MODIFY_AUDIO_SETTINGS and RECORD_AUDIO. No audio device will be available for recording

Answer

On Xamarin.Forms, you could use Permissions Plugin. https://github.com/jamesmontemagno/PermissionsPlugin

Add permission in Manifest file:

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

Create a Utils.cs

public static class Utils
{
public static async Task<PermissionStatus> CheckPermissions(Permission permission)
{
    var permissionStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(permission);
    bool request = false;
    if (permissionStatus == PermissionStatus.Denied)
    {
        if (Device.RuntimePlatform == Device.iOS)
        {

            var title = $"{permission} Permission";
            var question = $"To use this plugin the {permission} permission is required. Please go into Settings and turn on {permission} for the app.";
            var positive = "Settings";
            var negative = "Maybe Later";
            var task = Application.Current?.MainPage?.DisplayAlert(title, question, positive, negative);
            if (task == null)
                return permissionStatus;

            var result = await task;
            if (result)
            {
                CrossPermissions.Current.OpenAppSettings();
            }

            return permissionStatus;
        }

        request = true;

    }

    if (request || permissionStatus != PermissionStatus.Granted)
    {
        var newStatus = await CrossPermissions.Current.RequestPermissionsAsync(permission);

        if (!newStatus.ContainsKey(permission))
        {
            return permissionStatus;
        }

        permissionStatus = newStatus[permission];

        if (newStatus[permission] != PermissionStatus.Granted)
        {
            permissionStatus = newStatus[permission];
            var title = $"{permission} Permission";
            var question = $"To use the plugin the {permission} permission is required.";
            var positive = "Settings";
            var negative = "Maybe Later";
            var task = Application.Current?.MainPage?.DisplayAlert(title, question, positive, negative);
            if (task == null)
                return permissionStatus;

            var result = await task;
            if (result)
            {
                CrossPermissions.Current.OpenAppSettings();
            }
            return permissionStatus;
        }
    }

    return permissionStatus;
 }
}

Uasge:

var status = PermissionStatus.Unknown;

    status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Camera);

    if (status != PermissionStatus.Granted)
    {

        status = await Utils.CheckPermissions(Permission.Camera);
    }

Here is a code sample i done before about how to request permission of camera. Xamarin – Requesting camera permissions in WebView



Source: stackoverflow