Get the user's geographic location information regularly in ios

Dai Jax 1 Reputation point
2022-09-22T06:54:27.157+00:00

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

.NET MAUI
.NET MAUI
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
    2022-09-23T06:22:33.523+00:00

    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 (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.