Mierducci Castaneda
Mierducci Castaneda

Reputation: 61

Problem creating a pop-up alert before app requests location permission

I'm a beginner programmer and Google sent me an email saying

Your app must display a prominent disclosure through a pop-up alert before your app’s location runtime permission. Based on our review, a prominent disclosure did not appear before the runtime permission. Please add a prominent disclosure before the runtime permission.

I have tried a lot of things and just cant figure out how to popup an alert before the permissions. It always pops up up after the permissions.

Here is my MainActivity :

using Android;
using Android.App;
using Android.Content;
using Android.Webkit;
using Android.OS;
using Android.Content.PM;
using Xamarin.Essentials;
using Plugin.Permissions;

[assembly: UsesPermission(Name = Manifest.Permission.AccessFineLocation)]
[assembly: UsesPermission(Name = Manifest.Permission.AccessCoarseLocation)]
[assembly: UsesPermission(Name = Manifest.Permission.AccessBackgroundLocation)]
[assembly: UsesPermission(Name = Manifest.Permission.LocationHardware)]
[assembly: UsesPermission(Name = Manifest.Permission.Internet)]

namespace TqlSaldo.Droid
{
    [Activity(Label = "TqlSaldo", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)]

    public class WebViewLocationActivity : Activity
    {
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
        {
            PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            Plugin.Permissions.PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults); base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }


        protected override void OnCreate(Bundle bundle)
        {

            Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity = this;
            base.OnCreate(bundle);
            if (CheckSelfPermission(Manifest.Permission.AccessCoarseLocation) != (int)Permission.Granted)
            {
                RequestPermissions(new string[] { Manifest.Permission.AccessCoarseLocation, Manifest.Permission.AccessFineLocation }, 0);
            }
            Xamarin.Essentials.Platform.Init(this, bundle);
            var webview = new WebView(this);
            webview.Settings.JavaScriptEnabled = true;
            webview.Settings.SetGeolocationEnabled(true);            
            webview.SetWebChromeClient(new MyWebChromeClient(this));
            
            webview.LoadUrl("https://clientescentro.tanquelleno.com/Mapa/IndexLimite");

            SetContentView(webview);
        }
    }





    public class MyWebChromeClient : WebChromeClient
    {

        private readonly Context _context;

        public MyWebChromeClient(Context context)
        {
            _context = context;
        }

        public override void OnGeolocationPermissionsShowPrompt(string origin, GeolocationPermissions.ICallback callback)
        {
            const bool remember = true;
            var builder = new AlertDialog.Builder(_context);
            builder.SetTitle("Localizador de Estaciones")
                .SetMessage(string.Format("Por favor acepta lalocalizacion:", origin))
                .SetPositiveButton("Aceptar", (sender, args) => callback.Invoke(origin, true, remember))
                .SetNegativeButton("Denegar", (sender, args) => callback.Invoke(origin, false, remember));
            var alert = builder.Create();
            alert.Show();
        }



    }






}

And here's a screenshot of the GPS popup. the permissions are running fine i just need help por the prominent disclosure BEFORE the ask of permisions

The permissions are running fine, I just need help with the prominent disclosure BEFORE the ask of permissions

Upvotes: 6

Views: 10080

Answers (2)

Saamer
Saamer

Reputation: 5119

You are making a mistake by asking for permission inside your OnCreate() before the web view gets called. So, instead show the disclosure message first. Basically, you just need to replace this

if (CheckSelfPermission(Manifest.Permission.AccessCoarseLocation) != (int)Permission.Granted)
{
    RequestPermissions(new string[] { Manifest.Permission.AccessCoarseLocation, Manifest.Permission.AccessFineLocation }, 0);
}

with this

RequestLocationWithDisclosure();

And then just add these in your WebViewLocationActivity:

private void GetPermission()
{
    if (CheckSelfPermission(Manifest.Permission.AccessCoarseLocation) != (int)Permission.Granted)
    {
        RequestPermissions(new string[] { Manifest.Permission.AccessCoarseLocation, Manifest.Permission.AccessFineLocation }, 0);
    }
}

private void RequestLocationWithDisclosure()
{
    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.SetTitle("GPS Disclosure");
    alert.SetMessage(string.Format("Por favor acepta lalocalizacion:", origin));
    alert.SetPositiveButton("OK", (sender, e) => { GetPermission(); });

    var dialog = alert.Create();
    dialog.Show();
}

The above answer is for Xamarin Native, Xamarin Forms users can just use the following. The Forms function does not have the ability to call another function after you hit ok, unlike native android. So you just have to call them separately:

await App.Current.MainPage.DisplayAlert("GPS Disclosure", "This app collects location data to provide GPS information to the XYZ Company portal even when the app is closed or not in use.", "OK");

And then call this code

_location = await Geolocation.GetLocationAsync();

Upvotes: 3

Leo Zhu
Leo Zhu

Reputation: 15031

onGeolocationShowPrompt doesn't actually show a prompt itself, it is just a method that gets called when the page requests your Geolocation.

About Prominent Disclosure ,you could look at Google's Prominent Disclosure Requirement and User Data.

And you could declared permissions and in-app disclosures follow the video.

Upvotes: 0

Related Questions