Search code examples
c#xamarinmvvmcross

How to implement Permission request in Xamarin MvvmCross?


I'm trying to ask for permission using MvvmCross, but I don't see where I can set the initializations. Do you know any plugins that I can call in the .core project and use it in each mobile project?

This is my model distribution:

enter image description here

How you can see, I don't have any MainActivity class, instead, I have a Setup class and this is his build:

>         using Core;
>         using Core.Interfaces;
>         using MvvmCross;
>         using MvvmCross.Platforms.Android.Core;
>         using Services;
>         using System.Collections.Generic;
>         using System.Linq;
>         using System.Reflection;
>     
>         public class Setup : MvxAndroidSetup<App>
>         {
>             protected override void InitializeFirstChance()
>             {
>                 Mvx.IoCProvider.RegisterType<IDialogService, DialogService>();
>     
>                 base.InitializeFirstChance();
>             }
>     
>             public override IEnumerable<Assembly> GetPluginAssemblies()
>             {
>                 var assemblies = base.GetPluginAssemblies().ToList();
>                 assemblies.Add(typeof(MvvmCross.Plugin.Visibility.Platforms.Android.Plugin).Assembly);
>                 return assemblies;
>             }
>         }
>     

And this is my App class:

> using MvvmCross.IoC;
>     using MvvmCross.ViewModels;
>     using Plugin.Permissions;
>     using Plugin.Permissions.Abstractions;
>     using ViewModels;
> 
>     public class App : MvxApplication
>     {
>         public override void Initialize()
>         {
> 
>             this.CreatableTypes()
>                 .EndingWith("Service")
>                 .AsInterfaces()
>                 .RegisterAsLazySingleton();
> 
> 
> 
>             this.RegisterAppStart<GPSLocationViewModel>();
>         }
>     }

How can I set the permissions request for any device component (camera, location, etc)?


Solution

  • for android in general, please see Permissions overview

    for location, please see Get the last known location

    for example, for camera: specify needed permissions and ask user to grant permission

        [assembly: UsesPermission(Name = Android.Manifest.Permission.ReadExternalStorage)]
        [assembly: UsesPermission(Name = Android.Manifest.Permission.WriteExternalStorage)]
    
        ...
        ...
        ...
        public static readonly int PickImageActivityId = 91;
        public static readonly int RequestPermission_WriteExternalStorage = 92;
        static string[] Permissions = {
            Android.Manifest.Permission.ReadExternalStorage,
            Android.Manifest.Permission.WriteExternalStorage
        };
    
        ...
    
        if (FragmentController.Activity.CheckSelfPermission(Android.Manifest.Permission.WriteExternalStorage) != (int)Android.Content.PM.Permission.Granted)
            {
                FragmentController.RequestPermissions(Permissions, RequestPermission_WriteExternalStorage);
            }
            else
            {
                //already granted?
            }
        ...
        ...
    

    your activity will receive callback after image selected on

        OnActivityResult(int requestCode, int resultCode, Intent data)
        {
                if (requestCode == 91)
                {
                        if ((resultCode == (int)Result.Ok))
                        {
                            //data contains image
                        }
                        return;
                    }
        }