Rohit Chauhan
Rohit Chauhan

Reputation: 41

Camera and location permission at runtime in Xamarin forms

I have created a Xamarin forms application and it requires two permission i.e i)Location and ii)Camera.
I need to get both permissions at runtime but unfortunately I am able to get only one permission at a time when app starts.

Is there any solution to request both permission simultaneously at runtime.

Upvotes: 3

Views: 3853

Answers (1)

Juan Sturla
Juan Sturla

Reputation: 1304

It's a good advice not to ask for permissions when the application starts, just because the user might not know what is it for.

Also check and ask for permission when you need it. For example, ask for Camera permission just before opening the camera.

You can do all this using Xamarin.Essentials. If your project does not have it, follow the documentation instructions to set it up.

And then you can do everything in your shared code (for all platforms!)

There are tons of examples, I will write one. You can ask for the permissions one after the other, or in two different moments

public async void GoToCameraPage()
        {
            var status = await Permissions.CheckStatusAsync<Permissions.Camera>();
            if (status == PermissionStatus.Granted)
            {
                //We have permission!
                await Navigation.PushAsync(new CameraPage());
            }
            else
            {
                //Let the user know why
                await DisplayAlert("Permission needed", "I will need Camera permission for this action", "Ok");
                //Ask for the permission
                status = await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
                if (status == PermissionStatus.Granted)
                {
                    //YES, now we have permission
                }
                else
                {
                    //Ok, maybe I will ask again
                }
            }
        }

Upvotes: 5

Related Questions