Get the user's geographic location information regularly in ios

Dai Jax 1 Reputation point

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

A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,977 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 27,126 Reputation points Microsoft Vendor

    Hello @Dai Jax ,

    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 (  
                Manager = new LocationManager();// Please see the  LocationManager class( and LocationUpdatedEventArgs class ( , and add them to your project   

    Third, you can invoke the API defined above.

    public MainPage()  
            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.