Serving Industries Worldwide

Innovative Ways - Satisfied Clientele

Xamarin.Essentials: With Permission, App Theme, and Authentication


Listening is fun too.

Straighten your back and cherish with coffee - PLAY !

 
 

Xamarin_Essentials

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:

  1. Unknown shows permission is in an unknown state.
  2. Denied shows that the user denied permission.
  3. Disable shows that feature is disabled on the device.
  4. Granted shows user granted permission or permission is automatically granted. Restricted shows permission is in a restricted state.
   

This API uses runtime permission on Android, so Xamarin.Essentials must be initialized in our application. It is initialized into the OnCreate method in Android projects mainLuancher or any activity that launches Xamarin.Essentials.

Xamarin.Essentials.Platform.Init(this, savedInstanceState);

On Android, Xamarin.Essentials must receive OnRequestPermissionResult to handle run time permission. Following code will be added into all Activity classes.

public override void OnRequestPermissionsResult(int requestCode, 
string[] permissions, Android.Content.PM.Permission[] grantResults)
{
    Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

    base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}

Reference must be added to our class: “using Xamarin.Essentials;”

This image shows currently available permission for various platforms.

permission1

Image: Available permission for platforms (ref:https://docs.microsoft.com/en-us/xamarin/essentials/permissions?tabs=android)

App Theme: To check what user has selected as their device default theme should be easy, that’s why Xamarin.Essentials added a new feature to select the requested theme. RequestedTheme API provides information about the requested theme for running the app by the system. RequestedTheme is a part of the AppInfo class. AppInfo class provides information about our application. To use RequestedTheme first of all Xamarin.Essentials must be installed and set into the project, and the reference of Xamarin.Essentials must be added to the class as we added in permission. We can detect the requested application theme with this API: AppTheme appTheme = AppInfo.RequestedTheme; This will provide a current requested theme, which is by the system for the application. It will return one of these values: Unspecified, Light, Dark.

Authentication: One newer feature Xamarin.Essentials added that is web authentication. We can easily add authentication to a mobile app using this feature. It is paired with ASP.NET core. Many apps require adding user authentication, which means allowing users to sign in with their existing Microsoft, Facebook, Google, and now also with Apple Sign-in accounts. MSAL (Microsoft Authentication Library) provides a solution to add authentication to the application and also supports Xamarin apps in their Client NuGet Package. With the use of WebAuthenticator we can implement client-side functionality and also use our own web services. As we know this one is also a feature of Xamarin.Essentials so Xamarin.Essentials must be installed and set to the project and references must be added to the class. This API contains AuthenticatesAsync method and it will take two parameters URL and Uri. The WebAuthenticator API launch the URL in the browser and waits up to the callback received.

Azure vs AWS

Image: Working of Web Authenticator (ref:https://docs.microsoft.com/en-us/xamarin/essentials/web-authenticator?c ontext=xamarin%2Fandroid&tabs=android )

It is possible to use WebAuthenticator API with any back-end services. We have to configure our application with the following steps to use it with ASP.NET Core.

 

Planning to Hire Xamarin App Development Company ?

Your Search ends here.

 
  1. Set external social authentication providers (Facebook, Google, Microsoft) in ASP.NET Core web application.
  2. Set default Authentication scheme CookieAuthenticationDefaults.AuthenticationScheme in .AddAuthentication() call.
  3. Use .Add Cookie () in .AddAuthentication().
  4. All providers configure with. SaveTokens=True; is must.

The flow which the user has been chosen like to sign in through Microsoft or Facebook account is usually desirable to initiate the flow with mobile authentication flow. It is also important to return relevant information at a specific callback URI to end the authentication flow. To achieve this, use a custom API controller. The main purpose of creating this controller is to infer the provider that the app is requesting and start the authentication flow with the social provider (Facebook, Google) when the provider callback to the back-end controller it returns a result and redirects to the callback URI of the app.

Conclusion

In this blog, we have discussed Xamarin.Essentials and few features of Xamarin.Essentials. With the new release of Xamarin.Essentials, cross-platform features are unlocked. Here, we have discussed Permission, App Theme, and Authentication features. Xamarin.Essentials provide cross-platform APIs that work with Xamarin.Forms and all three platforms. We have also seen how to use permission, App Theme, and Authentication APIs. With the help of Xamarin.Essentials we can achieve these all three features.

Xamarin_Essentials

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:

  1. Unknown shows permission is in an unknown state.
  2. Denied shows that the user denied permission.
  3. Disable shows that feature is disabled on the device.
  4. Granted shows user granted permission or permission is automatically granted. Restricted shows permission is in a restricted state.
   

This API uses runtime permission on Android, so Xamarin.Essentials must be initialized in our application. It is initialized into the OnCreate method in Android projects mainLuancher or any activity that launches Xamarin.Essentials.

Xamarin.Essentials.Platform.Init(this, savedInstanceState);

On Android, Xamarin.Essentials must receive OnRequestPermissionResult to handle run time permission. Following code will be added into all Activity classes.

public override void OnRequestPermissionsResult(int requestCode, 
string[] permissions, Android.Content.PM.Permission[] grantResults)
{
    Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

    base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}

Reference must be added to our class: “using Xamarin.Essentials;”

This image shows currently available permission for various platforms.

permission1

Image: Available permission for platforms (ref:https://docs.microsoft.com/en-us/xamarin/essentials/permissions?tabs=android)

App Theme: To check what user has selected as their device default theme should be easy, that’s why Xamarin.Essentials added a new feature to select the requested theme. RequestedTheme API provides information about the requested theme for running the app by the system. RequestedTheme is a part of the AppInfo class. AppInfo class provides information about our application. To use RequestedTheme first of all Xamarin.Essentials must be installed and set into the project, and the reference of Xamarin.Essentials must be added to the class as we added in permission. We can detect the requested application theme with this API: AppTheme appTheme = AppInfo.RequestedTheme; This will provide a current requested theme, which is by the system for the application. It will return one of these values: Unspecified, Light, Dark.

Authentication: One newer feature Xamarin.Essentials added that is web authentication. We can easily add authentication to a mobile app using this feature. It is paired with ASP.NET core. Many apps require adding user authentication, which means allowing users to sign in with their existing Microsoft, Facebook, Google, and now also with Apple Sign-in accounts. MSAL (Microsoft Authentication Library) provides a solution to add authentication to the application and also supports Xamarin apps in their Client NuGet Package. With the use of WebAuthenticator we can implement client-side functionality and also use our own web services. As we know this one is also a feature of Xamarin.Essentials so Xamarin.Essentials must be installed and set to the project and references must be added to the class. This API contains AuthenticatesAsync method and it will take two parameters URL and Uri. The WebAuthenticator API launch the URL in the browser and waits up to the callback received.

Azure vs AWS

Image: Working of Web Authenticator (ref:https://docs.microsoft.com/en-us/xamarin/essentials/web-authenticator?c ontext=xamarin%2Fandroid&tabs=android )

It is possible to use WebAuthenticator API with any back-end services. We have to configure our application with the following steps to use it with ASP.NET Core.

 

Planning to Hire Xamarin App Development Company ?

Your Search ends here.

 
  1. Set external social authentication providers (Facebook, Google, Microsoft) in ASP.NET Core web application.
  2. Set default Authentication scheme CookieAuthenticationDefaults.AuthenticationScheme in .AddAuthentication() call.
  3. Use .Add Cookie () in .AddAuthentication().
  4. All providers configure with. SaveTokens=True; is must.

The flow which the user has been chosen like to sign in through Microsoft or Facebook account is usually desirable to initiate the flow with mobile authentication flow. It is also important to return relevant information at a specific callback URI to end the authentication flow. To achieve this, use a custom API controller. The main purpose of creating this controller is to infer the provider that the app is requesting and start the authentication flow with the social provider (Facebook, Google) when the provider callback to the back-end controller it returns a result and redirects to the callback URI of the app.

Conclusion

In this blog, we have discussed Xamarin.Essentials and few features of Xamarin.Essentials. With the new release of Xamarin.Essentials, cross-platform features are unlocked. Here, we have discussed Permission, App Theme, and Authentication features. Xamarin.Essentials provide cross-platform APIs that work with Xamarin.Forms and all three platforms. We have also seen how to use permission, App Theme, and Authentication APIs. With the help of Xamarin.Essentials we can achieve these all three features.