Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to get the user's permission to use more than one feature.

Will.ItraxWill.Itrax CAMember ✭✭✭

In our App I need to ask the user to use several features of their device. For now, let's focus on requesting two Permissions and getting two replies.

In a ViewModel I request permission for the StorageRead and StorageWrite on successive lines of code like this:

bool bOK = await Util.GetStorageReadPermissionAsync().ConfigureAwait(false);
bool bOL = await Util.GetStorageWritePermissionAsync().ConfigureAwait(false);

Those lines in turn call a couple methods (that surely can be improved, but I need to get them to work first).
Typically what happens is that the first one (GetStorageReadPermissionAsync) works (displays a request for access) and the second one (GetStorageWritePermissionAsync) does not.

Here are the methods that do the requesting:

        public static async Task<bool> GetStorageReadPermissionAsync()
        {
            PermissionStatus status = PermissionStatus.Unknown;
            try
            {
                status = await Xamarin.Essentials.Permissions.CheckStatusAsync<Xamarin.Essentials.Permissions.StorageRead>();
                if (status != PermissionStatus.Granted)
                {
                    MainThread.BeginInvokeOnMainThread(async () =>
                    {
                        status = await Xamarin.Essentials.Permissions.RequestAsync<Xamarin.Essentials.Permissions.StorageRead>();
                    });
                }
            }
            catch (Exception ex)
            {
                string msg = string.Format("GetStorageReadPermissionAsync {0}. ", ex.ToString());
                Debug.WriteLine(msg);
                Util.WriteExceptionToSQL(msg);
            }
            bool res = status == PermissionStatus.Granted ? true : false;
            return res;
        }

        public static async Task<bool> GetStorageWritePermissionAsync()
        {
            PermissionStatus status = PermissionStatus.Unknown;
            try
            {
                status = await Xamarin.Essentials.Permissions.CheckStatusAsync<Xamarin.Essentials.Permissions.StorageWrite>();
                if (status != PermissionStatus.Granted)
                {
                    MainThread.BeginInvokeOnMainThread(async () =>
                    {
                        status = await Xamarin.Essentials.Permissions.RequestAsync<Xamarin.Essentials.Permissions.StorageWrite>();
                    });
                }
            }
            catch (Exception ex)
            {
                string msg = string.Format("GetStorageWritePermissionAsync {0}. ", ex.ToString());
                Debug.WriteLine(msg);
                Util.WriteExceptionToSQL(msg);
            }
            bool res = status == PermissionStatus.Granted ? true : false;
            return res;
        }       

Best Answers

Answers

  • Will.ItraxWill.Itrax CAMember ✭✭✭

    @jezh Thanks - that works well for Droid. Are there similar examples for iOS and UWP? Mostly I found cross-platform examples.

  • jezhjezh Member, Xamarin Team Xamurai
    edited May 7

    1.For ios, you can refer document iOS App Permission Usage Description Keys to add the special permission in file info-plist.

    For example:

    2.For uwp, you can refer document App capability declarations to add Capability elements in your app package manifest.

    You can just enable the Capability by checking the necessary capability just as follows:

  • jezhjezh Member, Xamarin Team Xamurai

    @Will.Itrax Hi Will.Itrax, hope you are doing well. I’d like to follow up with you to see if there is any progress. Thanks!

  • Will.ItraxWill.Itrax CAMember ✭✭✭

    @jezh Thanks for getting back to me. Somehow I had missed your post. Sorry.
    I guess I was unclear in my followup question. You had provided great code for requesting Permissions in Droid, programmatically. Are there similar ways of requesting permissions in iOS and UWP - or do we only need to put entries in the info.plist and the manifest? Those things I have done. Now, since your code so cleanly requests permission in Droid, currently, in my common code, I only request it for iOS and UWP. And with @JamesMontemagno 's revisions, they are so clean now. That may be all I need. Thanks.

  • Will.ItraxWill.Itrax CAMember ✭✭✭

    @JamesMontemagno Thanks for the guidance and the great explanations. That cleans up the snippets of code that I had gleaned from various sources - without fully understanding what they do.

  • jezhjezh Member, Xamarin Team Xamurai

    Congrats, since you have got the answer you need, could you please mark this thread to finish this topic? Thanks in advance.

Sign In or Register to comment.