MAUI — Native Mobile Location Updates
Native Android & iOS Location Updates in a couple of code-lines
Welcome back folks 👋. In today’s episode of how to cook MAUI, we will take a look at native Android & iOS implementation instead of using the IGeolocation
interface from MAUI.
First things first, permissions here are nothing new; we need all the same as shown in Geolocation MAUI Documentation:
The next step is deciding how we would like to implement it:
- we can go with interface and implementation on each platform.
- or we can go with partial class without interface. Here as well, we can split our implementation into different platform folders or even the same folder with a platform file extension.
Here if we have only a few Services going with Android & iOS folders looks overkill. But if we were going to have five or more services, that is must have because this folder is going to be too massive for easy navigation.
Also, I would not recommend using #if ANDROID and #if IOS
for code that requires more than a couple of lines on each platform because it quickly becomes too hard to maintain.
In our solution, we will go with partial service
in Services Folder with separate Android and iOS files.
Shared Logic of Location Service
Nothing complicated here; in our example, I would like to be notified of service changes, like provider status or initialization, and getting in separate event location updates.
Android Implementation of Location Service
We will request permissions for LocationWhenInUse
and send notifications if it’s not granted. Also, we will check if the Location
is Enabled
on the Device and if GPS Provider
is Available
.
iOS Implementation of Location Service
Pretty much the same as for Android implementation, but here we have an optional line with RequestAlwaysAuthorization
, which is unnecessary if we don’t plan to get location updates when your app is on Background.
Yep, that’s it! A simple solution is to show what we can use and how to set this up for the base scenario. Yeah, it has some more exciting things under the hood for each platform, but the knowing class you are using, you can find all information at Apple and Google Documentations.
Link to the repo with all source code: