Xamarin Forms Marshmallow Permissions

DragosBarbuDragosBarbu DKUniversity ✭✭

Hi,
I am trying to ask for location permission on Android 6 through the DependencyService. My problem is that I do not know how to handle the callback of the ActivityCompat.RequestPermissions (activity, permissionGroup, permissionRequestID);

I tried to follow this and this but since I do not have a class for the activity I can not use OnRequestPermissionsResult. A solution I tried was to extend the activity object at runtime but using ActivityCompat.IOnRequestPermissionsResultCallback says there is no override for OnRequestPermissionsResult

Best Answer

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    I think @JamesMontemagno has solved the problem for you. Take a look and let me know what f it's not useful

    https://github.com/jamesmontemagno/Xamarin.Plugins/blob/master/Permissions/README.md

  • DragosBarbuDragosBarbu DKUniversity ✭✭

    It doesn't solve the issue. I still get "marked as an override but no suitable method found to override" when using this:
    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)

  • DragosBarbuDragosBarbu DKUniversity ✭✭

    I am using Xlabs, so my code looks like this:
    public class MainActivity : XFormsApplicationDroid,ActivityCompat.IOnRequestPermissionsResultCallback

    I looked into the IOnRequestPermissionsResultCallback interface and I can see the method, so I am thinking something is wrong with Xamarin Studio. I have tried both writing override then look into the list and copy pasting, none worked.

  • DragosBarbuDragosBarbu DKUniversity ✭✭

    Ok so I managed to make it work. It was an attention error, since I use an interface I do not have to override the method, just to implement it. After modifying the class to extend FormsAppCompatActivity and back, I realised my error. Thanks for setting me on the right track :wink:

  • 15mgm1515mgm15 USMember ✭✭✭✭

    Like @AlessandroCaliaro said Jame montemagno has a great nugget package to handle Android 6.0 permissions, easy and quick solution.

  • RaphaelChiorlinRanieriRaphaelChiorlinRanieri BRMember ✭✭✭

    Hey... Im using James plugin to request permissions in Marshmallow... But I'm having this exactly same problem...
    I cant override the method:

    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
            {
                PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            }
    

    Because it seems to not exist... I receive the no suitable method warning...

    I use FormsApplicationActivity, because I cant use the AppCompat one, because I dont use a AppCompat Theme.

    Is there any way to override it without AppCompat?

  • RaphaelChiorlinRanieriRaphaelChiorlinRanieri BRMember ✭✭✭
    edited April 2016

    @DragosBarbu Did you just implemented this method on your interface?

  • DragosBarbuDragosBarbu DKUniversity ✭✭

    Sorry for late response,

    My class header looks like this:

    public class MainActivity : XFormsApplicationDroid,ActivityCompat.IOnRequestPermissionsResultCallback
    

    So yes, I implemented the method from this interface, thus no override was needed

  • @DragosBarbu said:
    It doesn't solve the issue. I still get "marked as an override but no suitable method found to override" when using this:
    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)

    Make sure that you compile using Android version 6.0

  • Felipe.0183Felipe.0183 USMember

    Make sure you have Marshmallow in your aplicaction Target Framework.
    Options->General

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Ok, now I have the same problem. I can not override OnRequestPermissionsResult. Which is the correct solution?
    Do I have to use FormsAppCompatActivity?
    @JamesMontemagno can you help me, thanks...

  • try to add this:

    using Permission = Android.Content.PM.Permission;

  • CarlosCMCarlosCM USMember ✭✭

    I'm facing the same problem (using @JamesMontemagno lib also). Any more info about this?

    Thanks!

  • AdamCardenasAdamCardenas USMember
    edited November 2017

    The following seems to build, still have to run to test if it fires correctly.

        public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity, ActivityCompat.IOnRequestPermissionsResultCallback
            {
    
                protected override void OnCreate(Bundle bundle)
                {
                    TabLayoutResource = Resource.Layout.Tabbar;
                    ToolbarResource = Resource.Layout.Toolbar;
    
                    base.OnCreate(bundle);
    
                    global::Xamarin.Forms.Forms.Init(this, bundle);
    
                    ....
    
                    LoadApplication(new App());
                }
    
                public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults) { 
                }
    
            }
    
Sign In or Register to comment.