Xamarin.Essentials simplify complex native features into a single cross-platform library. With the use of Xamarin.Essentials we can develop any type of app into iOS, Android, UWP, etc platforms. With the recent release of Xamarin.Essentials 1.5 more cross-platform capabilities are introduced. We will discuss few features of Xamarin.Essentials that we can access. We will discuss the App Theme, Authentication, and Permission features of Xamarin.Essentials.
Xamarin.Essentials provide a single cross-platform API, that works with any Xamarin.forms. It can be accessed from shared code. We have to add NuGet Package to our project to use Xamarin.Essentials. Add Xamarin.Essentials to project, in solution explorer, right-click on the solution then select Manage NuGet Packages after that Search Xamarin.Essentials and Install-Package into All projects: Android, iOS, UWP, and .Net Standard libraries. After installing the Package add this reference to c# class: using Xamarin.Essentials;
Xamarin.Essentials with Permission: Permission class is used to check and request run time permission. When we want to use Geo-location, contacts, or the camera in our application we need to ask for permission from the user. With the use of Xamarin.Essentials we can easily add permission to our application configuration with few lines of code.
CheckStatusAsync method with specific permission is used to check the current status of permission and RequestAsync with specific permission is used to request permission. If the user granted permission in past then this method will show Granted and will not display any dialog. It is good to check the status of permission before requesting. Every operating system returns a different default state If the user has never asked, iOS will return Unknown and other platforms will return Denied. There is no need to make other calls if the status is already granted. On iOS, status is Denied then we have to ask a user to change the permission and for Android, we have to call ShouldShowRational to detect if the user denied permission in past or not. When we are using CheckStatusAsync or RequestAsynce, PermissionStatus is used to decide what will be the next step.
hese PermissionStatus are used :
-
Unknown shows permission is in an unknown state.
-
Denied shows that the user denied permission.
-
Disable shows that feature is disabled on the device.
-
Granted shows user granted permission or permission is automatically granted. Restricted shows permission is in a restricted state.