Google Resolved: MISSING_PERMISSION_ACCESS_COARSE_LOCATION in Xamarin.Android (C# - Xaml) | SubramanyamRaju Xamarin & Windows App Dev Tutorials

Monday, 13 April 2020

Resolved: MISSING_PERMISSION_ACCESS_COARSE_LOCATION in Xamarin.Android (C# - Xaml)

Introduction:

Android considers accessing device location to be a secure permission, which typically requires the user to grant their permission to access the resource. The user may revoke this permission at any time. This means that a run time permission request should be performed prior to location permission.

Description:

Permission Model before M (API 23): Before API 23, the permission model was simpler to the developer but offered less control and security to the user – requested permissions are presented to the user before installing the application. The user needs to decide whether to give these permissions to the application and install it or to deny as a whole and don’t install the application. Permissions cannot be denied or granted after the installation. Thus developers were required only to declare all needed permissions in the manifest and then just forget; if the app is running then it has all the requested permissions.
Permission Model from M (API 23): With Android 6.0 Marshmallow (API 23), a new runtime permission model has been introduced. According to this model users are not prompted for permission at the time of installing the app, rather developers need to check for and request permission at runtime (i.e. before performing any action that may require the particular permission), users can then allow or deny the permission, users can also grant or revoke permission anytime from settings. Making the user experience very secure on an Android device. But inversely this feature imposes a great deal of effort on development. Therefore developers have to handle all the use cases.
So in xamarin from Android SDK API 23, if you are not request location permission at runtime. You will get below exception
"MISSING_PERMISSION_ACCESS_COARSE_LOCATION.
So to resolve above error, please follow below steps in your xamarin.android project

Step 1:

All Android apps must declare one of the two permissions for location permission in the AndroidManifest.xml . To identify the permissions, one of the following two uses-permission elements must be add to AndroidManifest.xml:
  1. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />  
  2. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  
Step 2:
Add below method in MainActivity.cs
  1. public bool CheckAppPermissions()  
  2.         {  
  3.             if ((int)Build.VERSION.SdkInt < 23)  
  4.             {  
  5.                 return true;  
  6.             }  
  7.   
  8.             if (!(ContextCompat.CheckSelfPermission(this, Manifest.Permission.AccessCoarseLocation) == (int)Permission.Granted) && !(ContextCompat.CheckSelfPermission(this, Manifest.Permission.AccessFineLocation) == (int)Permission.Granted))  
  9.             {  
  10.                 var permissions = new string[] { Manifest.Permission.AccessCoarseLocation, Manifest.Permission.AccessFineLocation };  
  11.                 ActivityCompat.RequestPermissions(this, permissions, REQUEST_FOLDER_PERMISSION);  
  12.                 return false;  
  13.             }  
  14.             return true;  
  15.   
  16.         }  

FeedBack Note: Please share your thoughts, what you think about this post, Is this post really helpful for you? I always welcome if you drop comments on this post and it would be impressive.


Follow me always at @Subramanyam_B

No comments:

Post a Comment

Search Engine Submission - AddMe