Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
lawrence_shaun_introducing_net_maui_build_and_deploy_crosspl.pdf
Скачиваний:
46
Добавлен:
26.06.2023
Размер:
5.15 Mб
Скачать

Chapter 11 Getting Specific

you covered for both scenarios, but you do need to be aware of how they work and any platform-specific differences. Let’s take a look at each to get a better understanding.

Permissions

A common theme I have been discussing in this book is how .NET MAUI does a lot of the heavy lifting when it comes to dealing with each supported platform. This continues with permissions because .NET MAUI abstracts a large number of permissions.

It is worth noting that every operating system is different. Not all require permissions for certain features. Refer to the Microsoft documentation on what .NET MAUI supports and what is required for each platform at https://learn.microsoft.com/dotnet/ maui/platform-integration/appmodel/permissions- available-permissions.

There are two key methods that enable you to interact with the permission system in .NET MAUI.

Checking the Status of a Permission

In order to check whether the user has already granted permission to your application, you can use the CheckStatusAsync method on the class. For your Weather widget, you need access to the devices geolocation information. You have two options in terms of the permission to use:

•\ LocationWhenInUse: This only allows the application to access the geolocation information while the app is open in the foreground.

336

Chapter 11 Getting Specific

•\ LocationAlways: This allows the application to also access the geolocation information even when the app is backgrounded. This can be particularly useful for exercise tracking applications that need to monitor the user’s movement.

You only need the LocationWhenInUse option for your application.

PermissionStatus status = await Permissions. CheckStatusAsync<Permissions.LocationWhenInUse>();

It is recommended that you check the status of the permission before requesting it to gain an understanding of whether the user has been asked before. On iOS, you are only allowed to ask once and then you are required to prompt the user to go to the Settings app and enable permission if they wish to change their mind. Sadly, Android provides a different approach and will return a status of Denied even if the user has not been prompted before. In this scenario you are then recommended to call ShouldShowRationale to check whether the user really has been prompted.

The possible values for PermissionStatus are as follows:

•\ Unknown: The permission is in an unknown state, or on iOS, the user has never been prompted.

•\ Denied: The user denied the permission request.

•\ Disabled: The feature is disabled on the device.

•\ Granted: The user granted permission or it is automatically granted.

•\ Restricted: In a restricted state

337

Chapter 11 Getting Specific

Requesting Permission

Once you have confirmed that the user has not been prompted with a permission request, you can proceed to prompting them by using the Permissions.RequestAsync method along with the specific permission to request. In your example, this will be the LocationWhenInUse permission.

PermissionStatus status = await Permissions.

RequestAsync<Permissions.LocationWhenInUse>();

It is worth noting that the RequestAsync method needs to be run on the main or UI thread. This is needed because it can result in presenting the built-in system UI in order to ask the user if they wish to give permission. Therefore, whenever you call Permissions.RequestAsync you must make sure your code is already running on the main thread with the MainThread.IsMainThread property, or you can dispatch out to the main thread with the MainThread.InvokeOnMainThreadAsync method.

It is considered best practice to only prompt the user for permission to use a specific feature when they first try to use that feature. This helps to provide context to the user around why the permission

is being requested. You may also find that the different platform providers (e.g., Apple, Google, and Microsoft) have different rules they apply when reviewing and approving the applications you submit to their stores. For this, I recommend working with the most restrictive rules to save yourself pain and effort.

338

Chapter 11 Getting Specific

Handling Permissions in Your Application

The following section of code comes recommended from the Microsoft documentation site at ­https://learn.microsoft.com/dotnet/maui/ platform-integration/appmodel/permissions?#example. It has been included and left unchanged as it helps to really highlight the differences between platforms.

First, create the new folder and class for this new piece of functionality. Call the folder Services. Add a new interface file and call it ILocationService.cs under the Services folder. The contents of this new interface should be updated to the following

namespace WidgetBoard.Services;

public interface ILocationService

{

Task<Location> GetLocationAsync();

}

This provides a definition of what a location service implementation will provide: an asynchronous method that will ultimately return a

Location object.

Next, create an implementation. Add a new class file under the Services folder and call it LocationService.cs. Modify the initial contents to the following:

namespace WidgetBoard.Services;

public class LocationService : ILocationService

{

}

339

Chapter 11 Getting Specific

Now that you have a blank class, you can add the method for handling permission requests ready for use.

private async Task<PermissionStatus> CheckAndRequestLocationPermission()

{

PermissionStatus status = await Permissions. CheckStatusAsync<Permissions.LocationWhenInUse>();

if (status == PermissionStatus.Granted)

{

return status;

}

if (status == PermissionStatus.Denied && DeviceInfo. Platform == DevicePlatform.iOS)

{

//Prompt the user to turn on in settings

//On iOS once a permission has been denied it may not be requested again from the application

return status;

}

if (Permissions.ShouldShowRationale<Permissions. LocationWhenInUse>())

{

// Prompt the user with additional information as to why the permission is needed

}

status = await Permissions.RequestAsync<Permissions. LocationWhenInUse>();

return status;

}

340