Xamarin.Forms WebView not working with WebRTC

前端 未结 2 1341
不知归路
不知归路 2021-01-28 18:39

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.

相关标签:
2条回答
  • 2021-01-28 19:13

    I have found the solution and faced other issues. Base on @Wendy-Zhang answer on Xamarin - Requesting camera permissions in WebView , it doesn't work for me if follow single solution, instead, I put those 2 solutions, that make the trick.

    0 讨论(0)
  • 2021-01-28 19:35

    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

    0 讨论(0)
提交回复
热议问题