Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

How to give camera and microphone permission to WebView with Xamarin.Forms on Android?

Ruvi1996Ruvi1996 Member ✭✭

I tried https://stackoverflow.com/questions/59771557/xamarin-requesting-camera-permissions-in-webview this and many other solutions but nothing worked for me. It is working fine with mobile web browser but within my webview app it doesn't work. I tried creating a CustomRenderer as well as creating the Utils.cs file with Plugins.Permissions but nothing worked. Please help.

webView.Source = "https://test.webrtc.org/";

Answers

  • YelinzhYelinzh Member, Xamarin Team Xamurai
    edited July 2020

    How to give camera and microphone permission to WebView with Xamarin.Forms on Android

    Make sure the permissions have been added for the project.

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

    If deploying the application on Android 6.0 or above, it requires to request the permission at runtime.

    if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.Camera) != (int)Permission.Granted)
                {
        RequestPermissions(new string[] { Manifest.Permission.Camera }, 0);
    }
                if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.RecordAudio) != (int)Permission.Granted)
                {
        RequestPermissions(new string[] { Manifest.Permission.RecordAudio }, 0);
    }
    

    We also need to create a custom WebView renderer to override the WebChromeClient's OnPermissionRequest method to grant the request.

    [assembly: ExportRenderer(typeof(CustomWebView), typeof(CustomWebViewRenderer))]
    namespace App19F_8.Droid
    {
        public class CustomWebViewRenderer : WebViewRenderer
        {
            public CustomWebViewRenderer(Context context) : base(context)
            {
            }
            protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
            {
                base.OnElementChanged(e);
    
                if (e.NewElement != null)
                {
                    Control.SetWebChromeClient(new MyWebChromeClient(MainActivity.Instance));
                    Control.Settings.JavaScriptEnabled = true;
                }
            }
        }
    
        public class MyWebChromeClient : WebChromeClient
        {
            Activity mActivity = null;
            public MyWebChromeClient(Activity activity)
            {
                mActivity = activity;
            }
            public override void OnPermissionRequest(PermissionRequest request)
            {
                mActivity.RunOnUiThread(() => {
                    request.Grant(request.GetResources());
                });
            }
        }
    }
    
  • Ruvi1996Ruvi1996 Member ✭✭

    @YelinZh Thank you very much for you reply. Can you please help me to understand what causes below errors please? :/

  • YelinzhYelinzh Member, Xamarin Team Xamurai
    edited July 2020

    Sorry for my mistake, it doesn't require the code in this case. I've modified the code, please check that. The 'MainActivity.Instance' is the custom property which is used to reference the MainActivity class.

     public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
        {
            public static MainActivity Instance { get; private set; }
            protected override void OnCreate(Bundle savedInstanceState)
            {
                ...
                base.OnCreate(savedInstanceState);
    
                Instance = this;
                global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
                LoadApplication(new App());
            }
    }
    
  • Ruvi1996Ruvi1996 Member ✭✭
    edited July 2020

    @YelinZh One more thing, Code below If deploying the application on Android 6.0 or above, it requires to request the permission at runtime.
    Where should I add it exactly?

  • YelinzhYelinzh Member, Xamarin Team Xamurai
    edited August 2020

    If deploying the application on Android 6.0 or above, it requires to request the permission at runtime. Where should I add it exactly?

    Request the permission in the MainActivity class? Check the codc:
    https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/permissions?tabs=windows#runtime-permission-checks-in-android-60

    You could also do this using the permission function of the Essentials library.
    https://docs.microsoft.com/en-us/xamarin/essentials/permissions?tabs=android

  • KevinhkKevinhk Member ✭✭

    @Yelinzh said:

    If deploying the application on Android 6.0 or above, it requires to request the permission at runtime. Where should I add it exactly?

    Request the permission in the MainActivity class? Check the codc:
    https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/permissions?tabs=windows#runtime-permission-checks-in-android-60

    You could also do this using the permission function of the Essentials library.
    https://docs.microsoft.com/en-us/xamarin/essentials/permissions?tabs=android

    thank you very much for this but any update about the ios?

Sign In or Register to comment.