question

DaiJax-6008 avatar image
0 Votes"
DaiJax-6008 asked WenyanZhang-MSFT commented

Get the user's geographic location information regularly in ios

I have a need to get the user's status and location information regularly when the app enters the background or the mobile phone screen is turned off. I checked the relevant documents of the MAUI and did not see the relevant information. I have no idea about this. I hope someone can help me

dotnet-maui
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

WenyanZhang-MSFT avatar image
0 Votes"
WenyanZhang-MSFT answered WenyanZhang-MSFT commented

Hello @DaiJax-6008 ,

About the iOS background location, you can refer to Walkthrough - Background Location in Xamarin.iOS. If you want to implement background location function in iOS with MAUI, you can access specific iOS platform APIs and invoke the platform code, refer to .NET MAUI invoking platform code - .NET MAUI | Microsoft Learn.

First, you can define the cross-platform API as a partial class that defines partial method signatures for any operations you want to invoke on iOS.

 namespace MAUILocationSample.Services
 {
     public partial class LocationService
     {
         public partial void StartLocation();//define an API to begin generating location updates
     }
 }

Second, you can implement the API on iOS under Platforms/iOS folder. To set up the app, you need open info.plist , add UIBackgroundModes, NSLocationAlwaysAndWhenInUseUsageDescription, NSLocationWhenInUseUsageDescription key. For more details, you can refer to the Application set up part in the doc and the source code in sample.

 namespace MAUILocationSample.Services
 {
     public partial class LocationService
     {
         public static LocationManager Manager { get; set; }
         public partial void StartLocation()
         {// you can check the source code (https://github.com/xamarin/ios-samples/blob/main/Location/Location/ViewController.cs#L21)
             Manager = new LocationManager();// Please see the  LocationManager class(https://github.com/xamarin/ios-samples/blob/main/Location/Location/LocationManager.cs) and LocationUpdatedEventArgs class (https://github.com/xamarin/ios-samples/blob/main/Location/Location/LocationUpdateEventArgs.cs) , and add them to your project 
             Manager.StartLocationUpdates();
         }}
 }

Third, you can invoke the API defined above.

 public MainPage()
       {
     InitializeComponent();
         LocationService locationservice = new LocationService();
         locationservice.StartLocation();// Calling the method for testing
     }

After that , you can debug the app with your physical iOS device(Background location can only be tested on physical device. If the user declines the authorization, you will not get the location information), and you can see the printed location information in the Application Output console.

Last but not least, you can check out the Xamarin.iOS sample, the lifecycle events are in AppDelegate.cs. For MAUI, you could call the ConfigureLifecycleEvents method on the MauiAppBuilder object in the CreateMauiapp method of your MauiProgram class, please see: App lifecycle - .NET MAUI | Microsoft Learn.
In addition, the UI updates and generating location updates are in ViewController of the Xamarin.iOS sample, you can try to adjust to implement a MAUI version according to your needs.

Best Regards,
Wenyan Zhang


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for your answer. I tried this background method before. He can really get the user's location change in the background, but when I turn off the mobile phone screen, he stops getting the user's location. I hope I can get the location after I turn off the screen. I don't know if I can do this. Thank you again.

0 Votes 0 ·

When you put the app in background mode for a while, is there a prompt to ask you to change the authorization (Allow xxx to also use your location even when you are not using the app? Keep Only While Using/Chang to Always Allow)? You could go to Settings , select your APP, then check if the Location authorization is " Always". I tested on the iPhone 13 for 40 minutes with the screen off, and the location information kept printing.
The system can terminate apps at any time to free up memory or other system resources. It may has been killed by the system if the authorization is correct and you can not get the location information. If so, you could try Significant Location Changes Service. It is a simpler, power-conserving option available for devices with cellular radios. For more details, you can refer to Updating a Xamarin.iOS App in the Background - Xamarin | Microsoft Learn


0 Votes 0 ·
DaiJax-6008 avatar image DaiJax-6008 WenyanZhang-MSFT ·

Thanks for your help. I'll try again

0 Votes 0 ·
Show more comments