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

Chapter 11 Getting Specific

The section you want to add is as follows:

<!-- PermissionError --> <VerticalStackLayout

IsVisible="{Binding State, Converter={StaticResource HasPermissionErrorConverter}}">

<Label

Text="Unable to retrieve location data" />

<Button

Text="Retry"

Command="{Binding LoadWeatherCommand}" />

</VerticalStackLayout>

Now that you have added all of the required bits of code to call into the Permissions and Geolocation APIs, you need to configure each of your supported platforms to enable the location permission.

Configuring Platform-Specific Components

This is where .NET MAUI stops holding your hand and requires you to do some work in the platform-specific folders. Many of the APIs that are provided by .NET MAUI, as detailed in this section of the documentation site at https://learn.microsoft.com/dotnet/maui/platformintegration/, have the potential to require some level of platform-­

specific setup. This will vary per platform. For example, for haptic support, only Android requires some setup, whereas for the Geolocation API, all platforms require some setup.

Thankfully .NET MAUI provides helpful exceptions and error messages if you miss any of the platform-specific setup and they usually indicate

the action required to fix the issue. Topics like this do make it imperative that you really test your application on each of the platforms you wish to support to verify that it behaves as expected.

346

Chapter 11 Getting Specific

Let’s set up each platform so that your app can fully support accessing the devices’ current location.

Android

Android requires several permissions and features to be configured in order for your application to use the LocationWhenInUse permission. You can configure them inside the Platforms/Android/MainApplication.cs file so open it and make the following additions in bold:

using Android.App; using Android.Runtime;

[assembly: UsesPermission(Android.Manifest.Permission. AccessCoarseLocation)]

[assembly: UsesPermission(Android.Manifest.Permission. AccessFineLocation)]

[assembly: UsesFeature("android.hardware.location", Required = false)]

[assembly: UsesFeature("android.hardware.location.gps", Required = false)]

[assembly: UsesFeature("android.hardware.location.network", Required = false)]

namespace WidgetBoard;

Note that the use of the assembly keyword requires that the attributes are applied at the assembly level and not on the class like the current [Application] attribute usage. For further reference on how to get started with geolocation, refer to the Microsoft documentation at https:// learn.microsoft.com/dotnet/maui/platform-integration/device/ geolocation?tabs=android-get-started.

347

Chapter 11 Getting Specific

If you run the application on Android now, you will see that the first time you add a Weather widget onto a board, the system will present the following popup to the user asking them to allow permission for your application to use the location feature. Figure 11-1 shows the result of running your application on Android.

Figure 11-1.  The application running on Android showing the permission prompt when a weather widget is first added to a board

348

Chapter 11 Getting Specific

iOS/Mac

Apple requires that you specify the reason your application wants to use the Geolocation feature in the process of defining that your application uses the feature. You can configure this by modifying the Platforms/iOS/ Info.plist and Platforms/MacCatalyst/Info.plist files for iOS and Mac Catalyst, respectively. Both files require the same change, so let’s open them and add the following lines in. Note that I am opting to edit the files inside Visual Studio Code as I find it provides a better editing experience. There is a built-in editor inside Visual Studio but I personally prefer to edit the XML directly. Add the following lines inside the <dict> element:

<key>NSLocationWhenInUseUsageDescription</key> <string>In order to provide accurate weather information.</string>

For further reference on how to get started with Geolocation refer to the Microsoft documentation at https://learn.microsoft.com/

dotnet/maui/platform-integration/device/geolocation?tabs=ios - get-started

If you run the application on iOS and macOS now, you will see that the first time you add a Weather widget onto a board, the system will present the following popup to the user asking them to allow permission for your application to use the location feature. Figure 11-2 shows the result of running the application on iOS.

349